I have one single number which is a guaranteed power of two (1,2,4,8,16,...etc). How can I get the "bit index" from this number?
Say, I get the number "8" -> The answer I seek is "3" (bit#3)
1 -> 0
2 -> 1
4 -> 2
8 -> 3
16 -> 4
32 -> 5
..etc...
Of course I can build an array or a dictionary (the key being the number, the value the bit#) of... say 16 indices to get from the value to the bit#,
I can also do a
int i = 0, counter = 1;
while (counter != needed_value) {
counter *= 2;
i++;
}
// now "i" contains my bit#
but is there a... more fancy way?