How about taking the log (base 2) of your number? Here is a (maybe too slow) way of doing this. t = bitvariable; i = -1; while (t) { t >>= 1; ++i; } It takes your number (bitvariable) and leaves log base 2 in i, assuming bitvariable contains exactly one on-bit.