LeetCode
Bit
Set x to 1: x OR 1
Set x to 0: x OR 0
Toggle x : x XOR 1
191 Number of bit
Method 1: bit mask - query and shift for all 32 bits
Method 2: Count the last LSB of 1, then remove it x & (x - 1) always turns the last 1-bit to 0, and keep the rest
338
LSB of Odd number always 1, so divide by 2 (right shift) always lost 1 bit.
5(101), rightshift >> 1, equals 2(10)
LSB of Even number always 0, so divide by 2 (right shift) is fine
6(110), rightshift >> 1, equals 3(011)
260
Find the least significant bit of n
n &= -n
ex:
n = 0110
-n = 1001 + 1 = 1010 (negate means flip bit then add 1 )