Bitwise Operators

Bitwise operators in JavaScript operate on 32-bit operands. Internally, JavaScript converts 64-bit floating point numbers into 32-bit signed integers before performing the operation, it then converts back to 64-bit numbers to return the result.

JavaScript uses the following bitwise operators:

Operator Name Description
& AND If both bits are 1, result is 1; otherwise 0.
| OR If either bit is 1, result is 1; otherwise 0.
^ XOR If bits are different, result is 1; otherwise 0.
~ NOT If bit is 0, result is 1; otherwise 0.
<< Zero fill left shift Pushes zeros in from right, leftmost bits fall off.
>> Signed right shift Pushes copies of leftmost bit in from left, rightmost bit falls off (preserves sign).
>>> Zero fill right shift Pushes zeros in from left, rightmost bits fall off.

Examples

AND

console.log(19 & 7); // Output: 3
// 10011 = 19
// 00111 = 7
// 00011 = 3

OR

console.log(19 | 7); // Output: 23
// 10011 = 19
// 00111 = 7
// 10111 = 23

XOR

console.log(19 ^ 7); // Output: 20
// 10011 = 19
// 00111 = 7
// 10100 = 20

NOT

Because integers are stored in two’s complement (to change the sign, invert the binary digits and add one) a ~ operation will change the sign of the number and change the absolute value by one.

console.log(~19); // Output: -20
// 00000000000000000000000000010011 = 19
// 11111111111111111111111111101100 = -20

Zero fill left shift

console.log(19 << 3); // Output: 152
// 00000000000000000000000000010011 = 19
// 00000000000000000000000010011000 = 152

Signed right shift

The >> operator preserves the sign of the operand by pushing copies of the leftmost bit in from the left.

console.log(19 >> 3); // Output: 2
// 00000000000000000000000000010011 = 19
// 00000000000000000000000000000010 = 2
console.log(-20 >> 3); // Output: -3
// 11111111111111111111111111101100 = -20
// 11111111111111111111111111111101 = -3

Zero fill right shift

The >>> operator does not preserve the sign. It pushes zeros in from the left, pushing the sign bit out of its leftmost position.

console.log(19 >>> 3); // Output: 2
// 00000000000000000000000000010011 = 19
// 00000000000000000000000000000010 = 2
console.log(-20 >>> 3); // Output: 536870909
// 11111111111111111111111111101100 = -20
// 00011111111111111111111111111101 = 536870909

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn JavaScript on Codecademy

Contributors