x & -x
: returns the least significant '1' bit of x.-1 >> n
: returns -1, since -1 is represented by all '1' bits.flags &= ~MASK
: reset the kth bit flag within flags using MASK equals to 100...00 (k length).(flags & MASK) != 0
: check if the kth bit is set. If it is set, return true.x = x & (x-1)
: from right to left, jump to next '1' bit of x.
-
Java:
Integer.bitCount(int)
Using O(1) time to check whether an integer n is a power of 2.
Check whether its binary form is zeros leading with a single '1' bit.
n & (n - 1) == 0
a ^= b
b ^= a
a ^= b
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as theHamming weight).
For example, the 32-bit integer ’11' has binary representation00000000000000000000000000001011
, so the function should return 3.
count = 0
while x:
count += 1
x = x & (x - 1) # e.g. 7 -> 6 -> 4 -> 0