JavaScript Bitwise
JavaScript Basics

JavaScript Bitwise

Operator Table

Operator Name Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill left shift Shifts left by pushing zeros in from the right and let the leftmost bits fall off
>> Signed right shift Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
>>> Zero fill right shift Shifts right by pushing zeros in from the left, and let the rightmost bits fall off

Examples

Operation Result Same as Result
6 & 3 2 0110 & 0011 0010
6 | 3 7 0110 | 0011 0111
~6 -7 ~0110 1001
6 << 2 24 0110 << 2 11000
6 ^ 3 5 0110 ^ 0011 0101
6 >> 1 3 0110 >> 1 0011
6 >>> 1 3 0110 >>> 1 0011

JavaScript Uses 32 bits for Bitwise Operations

JavaScript stores numbers as 64-bit floating-point numbers but performs all bitwise operations on 32-bit binary numbers. Before performing a bitwise operation, JavaScript converts numbers to 32-bit signed integers. After the operation, the result is converted back to 64-bit numbers.

The examples above use 4-bit unsigned binary numbers. Due to this, ~6 returns -7.

Decimal Binary
6 00000000000000000000000000000110
~6 (-7) 11111111111111111111111111111001

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:

Operation Result
1111 & 0000 0000
1111 & 0001 0001
1111 & 0010 0010
1111 & 0100 0100

Four-bit example:

Operation Result
0 | 0 0
0 | 1 1
1 | 0 1
1 | 1 1

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:

Operation Result
1111 | 0000 1111
1111 | 0001 1111
1111 | 0010 1111
1111 | 0100 1111

Four-bit example:

Operation Result
0 ^ 0 0
0 ^ 1 1
1 ^ 0 1
1 ^ 1 0

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:

Operation Result
1111 ^ 0000 1111
1111 ^ 0001 1110
1111 ^ 0010 1101
1111 ^ 0100 1011

Four-bit example:

Decimal Binary
6 00000000000000000000000000000110
~6 (-7) 11111111111111111111111111111001

JavaScript Bitwise NOT (~)

Example:

Decimal Binary
6 00000000000000000000000000000110
6 << 2 00000000000000000000000000011000 (24)

Example:

let x = ~6;

JavaScript Zero Fill Left Shift (<<)

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

Example:

Decimal Binary
-6 11111111111111111111111111111010
-6 >> 1 11111111111111111111111111111101 (-3)

Example:

let x = 6 << 2;

JavaScript Sign-Preserving Right 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.

Example:

Decimal Binary
6 00000000000000000000000000000110
6 >>> 1 00000000000000000000000000000011 (3)

Example:

let x = 6 >>> 1;

Binary Numbers

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

Binary Representation Decimal value
00000000000000000000000000000001 1
00000000000000000000000000000010 2
00000000000000000000000000000100 4
00000000000000000000000000001000 8
00000000000000000000000000010000 16
00000000000000000000000000100000 32
00000000000000000000000001000000 64

Setting a few more bits reveals the binary pattern:

Binary Representation Decimal value
00000000000000000000000000000110 6
00000000000000000000000000001101 13
00000000000000000000000000101101 45

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 Representation Decimal value
00000000000000000000000000000110 6
11111111111111111111111111111001 -7

Converting Decimal to Binary

Example:

function dec2bin(dec) {
  return (dec >>> 0).toString(2);
}

Converting Binary to Decimal

Example:

function bin2dec(bin) {
  return parseInt(bin, 2).toString(10);
}
Take a look into your desired course