Most programming languages support the following bitwise operations (note that the following numbers are all in binary):
Symbol
Description
&
Perform an AND operation on the corresponding bits of two numbers. The result is 1 if and only if both binary bits are 1.
i.e. 11010 & 1 = 0
|
Perform an OR operation on the corresponding bits of two numbers. When any binary bit is 1, the result is 1.
i.e. 11010 | 1 = 11011
^
Perform an XOR operation on the corresponding bits of two numbers. When the two binary bits are different (one is 1 and the other is 0), the result is 1.
i.e. 11010 ^ 1 = 11011
>>
Shifts the bits of a number right by the specified number of bits, filling the left side with the sign bit of the number (for signed integers).
<<
Shifts the binary digits of a number to the left by the specified number of bits and fills the right side with 0s.
~
Perform a "reverse" operation on each bit of a number, changing 0 to 1 and 1 to 0.
i.e. ~11010 = 00101
Low bit
x & ~x is defined as low bit which can be use to find next closest power of 2. Always be used in Binary Index Tree