Map a range of integers to a 64-bit integer Given a 64-bit integer x (which is always larger than 0), we wish to map it to an index so that:


when x is between 0..64, we map it to 0,
when x is between 65..128, we map it to 1,
when x is between 129..192, we map it to 2, etc.


Writing such a function get_index(x) is simple, consider say the following program:

#include &lt;iostream&gt;
#include &lt;cstdint&gt;
#include &lt;cmath&gt;
#include &lt;cassert&gt;

// For benchmarking
#include &lt;vector&gt;
#include &lt;algorithm&gt;

std::uint64_t get_index(std::uint64_t x)
{
    return std::ceil(x / 64.0) - 1;
}

int main()
{
    // To see the expected outcome.
    /*
    for (int i = 1; i &lt;= 129; ++i)
    {
        std::int64_t idx = get_index(i);
        std::cout &lt;&lt; "i : " &lt;&lt; i &lt;&lt; " = " &lt;&lt; idx &lt;&lt; "\n";
    }
    */

    // A small performance benchmark.
    // Just in case print something so that the compiler doesn't optimize things away.
    std::vector&lt;std::uint64_t&gt; dummy;
    const std::uint64_t MAX = 10000000;
    for (std::uint64_t i = 1; i &lt;= MAX; ++i)
    {
        dummy.push_back(get_index(i));
    }

    std::cout &lt;&lt; dummy.size() &lt;&lt; " " &lt;&lt; *max_element(dummy.begin(), dummy.end()) &lt;&lt; "\n";
}


Is there a (much) faster implementation for get_index(x)? Likely one that completely avoids floating-point math, ceilings etc.

For instance, I know that dividing by 64 is the same thing as bit-shifting x to the right by 6, but I'm not much of a bit hack wizard to figure out the rest without resorting to things that seem messy.
std::uint64_t get_index(std::uint64_t x)
{
    return 0u == x ? 0u : (x-1u)&gt;&gt; 6;
}


Will it faster, I doubt.

By the way, your code for x = 0 will yield -1, which is not same as your description. if you do not need x = 0 input, then you can remove the if statement as 

std::uint64_t get_index(std::uint64_t x)
{
    return (x-1u)&gt;&gt; 6;  // for this, x must be not 0u.
}