In C, C ++ and similar syntax, you can determine if the right-most bit in integer i 1 or 0, checking if i & 1 non-zero or zero. (Note that single & , meaning bitwise AND, and not && , meaning logical AND). For bits of second-to-right, you check i & 2 ; for the third you check i & 4 etc. degrees of two.
More generally, to determine if a bit is equal, that j th on the right is zero, you can check if i & (1 << (j-1)) != 0 . << indicates a left shift; 1 << (j-1) is essentially equivalent to 2 j-1 .
So for a 32-bit integer, your loop would look something like this:
unsigned int i = 26; int j; for (j = 31; j >= 0; j--) { if ((i & (1 << (j-1))) != 0) else }
Hope this is enough to get you started.
source share