Below are interactive examples of Javascript bitwise functions. Code in the first box is interpreted and the results are displayed in the second box.
Prefacing a string of binary digits with 0B or 0b denotes a binary number. Javascript will convert the number to integer format.
Javascript will normally output numbers in decimal format. Use toString(2) to get the binary format.
The AND operator (&) compares two numbers and returns a new number with 1's where corresponding bits in both of the inputs are also 1.
The OR operator (|) compares two numbers and returns a new number with 1's where corresponding bits in either of the inputs are also 1.
The XOR (exclusive OR) operator (^) compares two numbers and returns a new number with 1's where corresponding bits in either (but not both) of the inputs are 1.
The NOT operator (~) works on a single number and inverts the bits. Becuase of the way that Javascript represents signed integers, this has the effect adding 1 to the operand and negating it.
A left shift (<<) returns a new number with all of the bits in the operand shifted to the right a given number of places. The rightmost bits are dropped and the leftmost bits are filled with 0s.
A right shift (>>) returns a new number with all of the bits in the operand shifted to the right a given number of places. The rightmost bits are dropped and the leftmost bits are filled with 1s.
A right shift (>>>) returns a new number with all of the bits in the operand shifted to the right a given number of places. The rightmost bits are dropped and the leftmost bits are filled with 0s.