Consider this program
#include <iostream>
#include <bitset>
#include <cstdint>
#include <cstdlib>
typedef uint8_t Tnum;
template <typename T>
void printBits(T a)
{
std::cout << std::bitset<(sizeof(a) * 8)>(a).to_string() << '\n';
}
int main()
{
printBits(Tnum(15));
printBits(Tnum(17));
return EXIT_SUCCESS;
}
it prints
00001111
00010001
Now consider this 2 guys from the previous output
00001111
^
00010001
^
I would like to know how, given a signed or unsigned integer type, and given a value for an instance of that type, I can get the location of that leading 1
in the pattern, starting to count from 0
the result I expect is 3
for the first row, 4
for the second one. The total amount of positions involved is also acceptable to me, like 4
for the first row and 5
for the second one.
I don't have Hacker's Delight or similar text available at the moment and I can't find any quick bit twiddling .
This is kinda it but it's error prone and it will never pass a conversion test or set of warning flags about conversions, at least in my case. Plus it's probably a non-optimal choice.
Please no lookup tables, I'm willing to accept anything that doesn't cause conversion issues and doesn't use a LUT. For C89/99 and C++11 .