Given an unsigned int, I have to do the following operations:
- Count the number of bits set to 1
- Find the index of the leftmost 1 bit
- Find the index of the largest 1 bit
(operation should not be architecture dependent).
I did this with a bit shift, but I need to go through almost all the bits (see 32). For example, counting 1:
unsigned int number= ...; while(number != 0){ if ((number & 0x01) != 0) ++count; number >>=1; }
Other operations are similar.
So my question is: is there a faster way to do this?
source share