A demonstration of Javascript Bitwise Functions.

Below are interactive examples of Javascript bitwise functions. Code in the first box is interpreted and the results are displayed in the second box. Only the final value is displayed in the 2nd box. If you want to show more than 1 value, concatenate multiple values into a single string.

Binary Format

Prefacing a string of binary digits with 0B or 0b denotes a binary number. Javascript will convert the number to integer format.

Converting

Javascript will normally output numbers in decimal format. Use toString(2) to get the binary format.

AND

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.

OR

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.

XOR

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.

NOT

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.

Left Shift

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.

Right Shift

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.

Zero Fill Right Shift

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.