Open Playground
JS TutorialLesson #38

JavaScript Bitwise Operations

JavaScript stores numbers as 64 bits floating point numbers, but all bitwise operations are performed on 32 bits binary numbers.

JavaScript Bitwise Operators

OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
\ORSets each bit to 1 if one of two bits is 1
^XORSets each bit to 1 if only one of two bits is 1
~NOTInverts all the bits
>Signed right shiftShifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
>>>Zero fill right shiftShifts right by pushing zeros in from the left, and let the rightmost bits fall off

Examples

OperationResultSame asResult
5 & 110101 & 00010001
5 \150101 \00010101
~ 510~01011010
5 > 120101 >> 10010
5 >>> 120101 >>> 10010

JavaScript Uses 32 bits Bitwise Operands

JavaScript stores numbers as 64 bits floating point numbers, but all bitwise operations are performed on 32 bits binary numbers.

Before a bitwise operation is performed, JavaScript converts numbers to 32 bits signed integers.

After the bitwise operation is performed, the result is converted back to 64 bits JavaScript numbers.

The examples above uses 4 bits unsigned binary numbers. Because of this ~ 5 returns 10. > Since JavaScript uses 32 bits signed integers, it will not return 10. It will return -6. > 00000000000000000000000000000101 (5) > 11111111111111111111111111111010 (~5 = -6) > A signed integer uses the leftmost bit as the minus sign.

JavaScript Bitwise AND

When a bitwise AND is performed on a pair of bits, it returns 1 if both bits are 1.

One bit example:

OperationResult
0 & 00
0 & 10
1 & 00
1 & 11

4 bits example:

OperationResult
1111 & 00000000
1111 & 00010001
1111 & 00100010
1111 & 01000100

JavaScript Bitwise OR

When a bitwise OR is performed on a pair of bits, it returns 1 if one of the bits is 1:

One bit example:

OperationResult
0 \00
0 \11
1 \01
1 \11

4 bits example:

OperationResult
1111 \00001111
1111 \00011111
1111 \00101111
1111 \01001111

JavaScript Bitwise XOR

When a bitwise XOR is performed on a pair of bits, it returns 1 if the bits are different:

One bit example:

OperationResult
0 ^ 00
0 ^ 11
1 ^ 01
1 ^ 10

4 bits example:

OperationResult
1111 ^ 00001111
1111 ^ 00011110
1111 ^ 00101101
1111 ^ 01001011

JavaScript Bitwise AND (&)

Bitwise AND returns 1 only if both bits are 1:

DecimalBinary
500000000000000000000000000000101
100000000000000000000000000000001
5 & 100000000000000000000000000000001 (1)

Example

Example Code

javascript
Try it Yourself
let x = 5 & 1;

JavaScript Bitwise OR (|)

Bitwise OR returns 1 if one of the bits is 1:

DecimalBinary
500000000000000000000000000000101
100000000000000000000000000000001
5 \100000000000000000000000000000101 (5)

Example

Example Code

javascript
Try it Yourself
let x = 5 | 1;

JavaScript Bitwise XOR (^)

Bitwise XOR returns 1 if the bits are different:

DecimalBinary
500000000000000000000000000000101
100000000000000000000000000000001
5 ^ 100000000000000000000000000000100 (4)

Example

Example Code

javascript
Try it Yourself
let x = 5 ^ 1;

JavaScript Bitwise NOT (~)

DecimalBinary
500000000000000000000000000000101
~511111111111111111111111111111010 (-6)

Example

Example Code

javascript
Try it Yourself
let x = ~5;

JavaScript (Zero Fill) Bitwise Left Shift ( >)

This is a sign preserving right shift. Copies of the leftmost bit are pushed in from the left, and the rightmost bits fall off:

DecimalBinary
-511111111111111111111111111111011
-5 >> 111111111111111111111111111111101 (-3)

Example

Example Code

javascript
Try it Yourself
let x = -5 >> 1;

JavaScript (Zero Fill) Right Shift (>>>)

This is a zero fill right shift. One or more zero bits are pushed in from the left, and the rightmost bits fall off:

DecimalBinary
500000000000000000000000000000101
5 >>> 100000000000000000000000000000010 (2)

Example

Example Code

javascript
Try it Yourself
let x = 5 >>> 1;

Shift Assignment Operators

OperatorExampleSame As>=x >>= yx = x >> y

The >= Operator

The Right Shift Assignment Operator right shifts a variable (signed).

Right Shift Assignment Example

Example Code

javascript
Try it Yourself
let x = -100;

x >>= 5;

The >>>= Operator

The Unsigned Right Shift Assignment Operator right shifts a variable (unsigned).

Unsigned Right Shift Assignment Example

Example Code

javascript
Try it Yourself
let x = -100;

x >>>= 5;

Bitwise Assignment Operators

OperatorExampleSame As&=x &= yx = x & y
^=x ^= yx = x ^ y
\=x \= yx = x \y

The &= Operator

The Bitwise AND Assignment Operator does a bitwise AND operation on two operands and assigns the result to the the variable.

Bitwise AND Assignment Example

Example Code

javascript
Try it Yourself
let x = 10;

x &= 5;

The |= Operator

The Bitwise OR Assignment Operator does a bitwise OR operation on two operands and assigns the result to the variable.

Bitwise OR Assignment Example

Example Code

javascript
Try it Yourself
let x = 10;

x |= 5;

The ^= Operator

The Bitwise XOR Assignment Operator does a bitwise XOR operation on two operands and assigns the result to the variable.

Bitwise XOR Assignment Example

Example Code

javascript
Try it Yourself
let x = 10;

x ^= 5;

Binary Numbers

Binary numbers with only one bit set are easy to understand:

Binary RepresentationDecimal value
000000000000000000000000000000011
000000000000000000000000000000102
000000000000000000000000000001004
000000000000000000000000000010008
0000000000000000000000000001000016
0000000000000000000000000010000032
0000000000000000000000000100000064

Setting a few more bits reveals the binary pattern:

Binary RepresentationDecimal value
000000000000000000000000000001015 (4 + 1)
0000000000000000000000000000110113 (8 + 4 + 1)
0000000000000000000000000010110145 (32 + 8 + 4 + 1)

JavaScript binary numbers are stored in two's complement format.

This means that a negative number is the bitwise NOT of the number plus 1:

Binary RepresentationDecimal value
000000000000000000000000000001015
11111111111111111111111111111011-5
000000000000000000000000000001106
11111111111111111111111111111010-6
0000000000000000000000000010100040
11111111111111111111111111011000-40
Joke: > There are only 10 types of people in the world: those who understand binary and those who don't.

Converting Decimal to Binary

Example

Example Code

javascript
Try it Yourself
function dec2bin(dec){

  return (dec >>> 0).toString(2);

}

Converting Binary to Decimal

Example

Example Code

javascript
Try it Yourself
function bin2dec(bin){

    return parseInt(bin, 2).toString(10);

}


Interactive Code Examples

JS Example 1

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let x = 5 & 1;

JS Example 2

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let x = 5 | 1;

JS Example 3

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let x = 5 ^ 1;

JS Example 4

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let x = ~5;

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let x = 5 << 1;

JS Example 6

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let x = -5 >> 1;