Find most significant bit without bitshifts I am trying to find the most significant bit of unsigned number without using any bitshifts. This is what I have come up with, which looks very naive but it's very simple:

template&lt;typename T&gt;
constexpr uint8_t find_msb(const T&amp; a_variable)
{
    static_assert(std::is_unsigned&lt;T&gt;::value, "Variable must be unsigned");

    constexpr uint8_t number_of_bits = std::numeric_limits&lt;T&gt;::digits;
    const std::bitset&lt;number_of_bits&gt; variable_bitset{a_variable};

    for (uint8_t msb = number_of_bits - 1; msb &gt;= 0; msb--)
    {
        if (variable_bitset[msb] == 1)
            return msb + 1;
    }        
    return 0;
}


In C++17 this is a valid constexpr function and it appears it just vanishes into thin air with gcc, even with -O0: https://godbolt.org/z/pKI8fj.

But the non constexpr version is more involved: https://godbolt.org/z/eyj2bO. 

In my use case I can use the constexpr version so it's not a big deal. How can I improve this code while maintaining readability and also without using any "smart" tricks and/or bitshifting?
use std::uint8_t, to avoid relying on something that may or may not exist in the global namespace.
An unsigned type will always be &gt;= 0. So if a_variable is 0, this will result in undefined behaviour by accessing something out of bounds of the bitset (and either crash or loop forever).
If we're finding the "most significant bit", I don't think avoiding bitshifts makes it more readable. (This requires understanding of both numeric_limits::digits, and std::bitset instead).