Set Bit Operations

ANDing a number with its two's complement isolates the lowest-order 1 bit. The expression num AND num minus 1 removes the rightmost set bit from num. Repeating rightmost-set-bit removal until zero and counting iterations gives the total nu…

1 sources - 4 claims

ANDing a number with its two's complement isolates the lowest-order 1 bit. The expression num AND num minus 1 removes the rightmost set bit from num. Repeating rightmost-set-bit removal until zero and counting iterations gives the total number of 1 bits. Extracting the rightmost set bit is useful for processing or removing set bits from the least significant end one at a time.