NVIDIA Interview Question
Assuming uint32 value.
Do a binary search.
Take masks and AND with the uint32.
Ex: uint16 value = 0000 0101 1100 0010
first step: mask the LSB 8 bit and MSB 8 bits
MSB: 0000 0101
LSB: 1100 0010
MSB > 0 => repeat first step on MSB
MSB: 0000
LSB: 0101
MSB == 0 => repeat first step on LSB
MSB: 01
LSB: 01
MSB > 0 => repeat first step on MSB
MSB: 0
LSB: 1
MSB == 0 => repeat first step on LSB
LSB == single bit. This is the MSB of uint16
The complexity is O(log n)
Please add your thoughts.
a & (((sizeof(a)*8)-1)<<1)
- Anonymous May 31, 2010