Wednesday, August 11, 2010

Bitwise Operations Tutorial


Common Bitwise Operations

x & (x-1) - Returns number x with the lowest bit set off. Also to check if an integer is a power of 2.
x ^ ( x & (x-1 )) - Returns the lowest bit of a number x.
x & 1 < < n - Returns 1 < < n is the n-th bit is set in x.
x | 1 < < n - Returns the number x with the n-th bit set.
x ^ 1 < < n - Toggles the state of the n-th bit in the number x.

No comments:

Post a Comment