Bitwise Operators


&

bitwise And = 1 if both bits are 1.

|

bitwise Or = 1 if either bit is 1.

||

bitwise Xor = 1 if both bits are different.

~

bitwise Not. Inverts the bits.

n << p

Shifts the bits of n left p positions. Zero bits are shifted into the low-order positions.

n >> p

Shifts the bits of n right p positions. If n is a 2's complement signed number, the sign bit is shifted into the high-order positions.

n >>> p

Shifts the bits of n right p positions. Zeros are shifted into the high-order positions.


All numbers are converted to 32 bit integers for all bitwise operations.


Examples:

3 & 5 = 1

3 | 5 = 7

3 || 5 = 6

~3 = -4

3 << 2 = 12

5 >> 2 = 1

-4 >>> 28 = 15






Become a Patron