Wed 30 Nov 2005

## Least Significant 1 Bit

Posted by pete under Uncategorized

Comments Off on Least Significant 1 Bit

This can be useful for extracting the lowest numbered element of a bit set. Given a 2’s complement binary integer value x, (x&-x) is the least significant 1 bit. The reason this works is that it is equivalent to (x & ((~x) + 1)); any trailing zero bits in x become ones in ~x, adding 1 to that carries into the following bit, and AND with x yields only the flipped bit… the original position of the least significant 1 bit.

Alternatively, since (x&(x-1)) is actually x stripped of its least significant 1 bit, the least significant 1 bit is also (x^(x&(x-1))).

## No Responses to “ Least Significant 1 Bit ”

Sorry, comments for this entry are closed at this time.