Common Bit Manipulations And Knowledge

  1. Set Negation: all_bits ^ A

  2. Get Lowest Set Bit of A Number x: x & ~(x - 1)

  3. Judge if a number is power of 2: n > 0 and n & -n (-n = ~(n - 1))

  4. Get lower bits of a number one by one: i = (i - 1) & n

  5. The & and | operators have lower precedence than comparison operators.

  6. If you want to write completely portable C/C++ code, be sure to use unsigned types, particularly if you plan to use the top-most bit.