Bitwise operators convert their operands into a binary number and operate on each bit. There are several operators available Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^), Bitwise NOT (~), Left Shift (<<), Sign-propagating right shift (>>) and Zero-fill right shift (>>>).
Bitwise operators operate on Binary numbers i.e. zeros & ones. Hence it is important to understand how they are stored to understand the Bitwise operators
A binary number is a number expressed in the base-2 numeral system or binary numeral system. It uses 0 (zero) and 1 (one) to represent the numbers.
For Example, the number 20 is represented as follows in the binary system
Decimal Binary
20 10100
You can convert a decimal number to a binary number using the toString method and passing the 2 as the radix.
console.log(Number(20).toString(2)) //10100
JavaScript uses 64-bit floating-point numbers to represent numbers. But when we use the Bitwise Operators, it converts them to 32-bit signed integers, performs the Bitwise operations, and converts them back to floating-point representation.
Signed 32-bit integers use the first bit to store the sign, and the remaining 31 bits to represent the numeric value of the integer. Hence number 20 in signed 32-bit binary is represented as below.
sign Remaining 31 Bits for Numbers
bit
0 0000000000000000000000000010100 Total 32 Bits
Sign bit
0 is positive number
1 is negative number
Since the most significant bit (leftmost) is the sign bit, which gives you only 31 bits for the number value. Hence the max value that can use with the Bitwise operators is 2147483647, above which you will not get the correct results.
The positive number is stored in a true binary format. But Negative numbers are stored in a format called two’s complement.
To get the two’s complement of an integer,
For Example to get the number 20, start with the binary representation of 20
+20 as binary
0 0000000000000000000000000010100
Invert the digits
1 1111111111111111111111111101011
Example
In the following example a > 5 is the first expression and b > 5 is the second expression. Since a > 5 is true, it evaluates the expression a > 5 and returns true
let a=10
let b=15
console.log(a > 5 || b > 5) //true
console.log(a > 5 || b < 5) //true although the b < 5 is false
Add 1 to the result
1 1111111111111111111111111101011
1 +
---------------------------------
1 1111111111111111111111111101100
---------------------------------
The result is how -20 is stored as a binary number in two complement
11111111111111111111111111101100
You can refer to the Binary to the Decimal converter to check the result. Make sure to choose the signed 32 bit in the dropdown.
If the number has more than 32-bit integers get their most significant bits discarded. For example, the following integer with more than 32 bits will be converted to a 32-bit integer
Before: 11100110111110100000000000000110000000000001
After: 10100000000000000110000000000001
You can chain multiple operands in a single statement.
The Bitwise AND operator accept two operands. It compares each binary digit of the left operand with the digit at the corresponding position in the right operand. If both the bits are 1, then it returns 1, else returns 0. The following table shows how the digits are compared
| a | b | a & b |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 1 | 1 |
Example
console.log(9 & 7) //1
console.log(-9 & -7) //-15
console.log(-9 & 7) //7
console.log(6 & 7) //6
console.log(37 & 23) //5
9 & 7 = 1
00000000000000000000000000001001 =9
00000000000000000000000000000111 =7
--------------------------------
00000000000000000000000000000001 =1
--------------------------------
-9 & 7 = 7
11111111111111111111111111110111 = -9
00000000000000000000000000000111 = 7
--------------------------------
00000000000000000000000000000111 = 7
--------------------------------
6 & 7 = 6
00000000000000000000000000000110 = 6
00000000000000000000000000000111 = 7
--------------------------------------
00000000000000000000000000000110 = 6
--------------------------------------
b
37 & 23 = 5
00000000000000000000000000100101 = 37
00000000000000000000000000010111 = 23
--------------------------------
00000000000000000000000000000101 = 5
--------------------------------
The Bitwise OR operator accepts two operands. It compares each binary digit of the left operand with the digit at the corresponding position in the right operand. If both the bits are 0, then it returns 0, else returns 1. The following table shows how the digits are compared
| a | b | a I b |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 1 | 1 |
Example
console.log(9 | 7) //15
console.log(-9 | -7) //-1
console.log(-9 | 7) //-9
console.log(6 | 7) //7
console.log(37 | 23) //55
9 | 7 = 1
00000000000000000000000000001001 =9
00000000000000000000000000000111 =7
--------------------------------
00000000000000000000000000001111 =1
5
--------------------------------
-9 & -7 = -15
11111111111111111111111111110111 = -9
11111111111111111111111111111001 = -7
--------------------------------
11111111111111111111111111111111 = -1
--------------------------------
-9 & 7 = 7
11111111111111111111111111110111 = -9
00000000000000000000000000000111 = 7
--------------------------------
11111111111111111111111111110111 = -9
--------------------------------
6 & 7 = 6
00000000000000000000000000000110 = 6
00000000000000000000000000000111 = 7
--------------------------------------
00000000000000000000000000000111 = 7
--------------------------------------
37 & 23 = 5
00000000000000000000000000100101 = 37
00000000000000000000000000010111 = 23
--------------------------------
00000000000000000000000000110111 = 55
--------------------------------
The Bitwise XOR operator accepts two operands. It compares each binary digit of the left operand with the digit at the corresponding position in the right operand. If both the bits are different then it returns 1, else 0. The following table shows how the digits are compared
| a | b | a^ b |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 1 | 0 |
Examples
console.log(9 ^ 7) //14
console.log(-9 ^ -7) //14
console.log(-9 ^ 7) //-16
console.log(6 ^ 7) //1
console.log(37 ^ 23) //50
9 | 7 = 1
4
00000000000000000000000000001001 =9
00000000000000000000000000000111 =7
--------------------------------
00000000000000000000000000001110 =1
4
--------------------------------
-9 & -7 = 14
11111111111111111111111111110111 = -9
11111111111111111111111111111001 = -7
--------------------------------
00000000000000000000000000001110 = 14
--------------------------------
-9 & 7 = -16
11111111111111111111111111110111 = -9
00000000000000000000000000000111 = 7
--------------------------------
11111111111111111111111111110000 = -16
--------------------------------
6 & 7 = 1
00000000000000000000000000000110 = 6
00000000000000000000000000000111 = 7
--------------------------------------
00000000000000000000000000000001 = 1
--------------------------------------
37 & 23 = 5
0
00000000000000000000000000100101 = 37
00000000000000000000000000010111 = 23
--------------------------------
00000000000000000000000000110010 = 50
--------------------------------
Bitwise NOT ~ is a unary operator, hence it takes only one operand. It just flips the binary digits from 1 to 0 & 0 to 1.
~5 = 6
00000000000000000000000000000101 = 5
11111111111111111111111111111010 = -6
~6 = 7
00000000000000000000000000000110 = 6
11111111111111111111111111111001 = -7
~-6 = 5
11111111111111111111111111111010 = -6
00000000000000000000000000000101 = 5
The left Shift operator shift (<<) the specified number of digits of the first operand to the left. The right operand specifies the number of digits to shift
Syntax
a << b
Where
a: The first operand, whose digits to shift left
b: Number of digits to shift.
Example
9
00000000000000000000000000001001 = 9
9 << 1
00000000000000000000000000010010 = 18
9 << 2
00000000000000000000000000100100 = 36
Negative Number
-9
11111111111111111111111111110111 = -9
-9 << 1
11111111111111111111111111101110 = -18
-9 << 2
11111111111111111111111111011100 = -36
The Right Shift operator (>>) shifts the specified number of digits of the first operand to the right. The right operand specifies the number of digits to shift
a >> b
Where
a: The first operand, whose digits to shift
right
b: Number of digits to shift
//+Ve Number
console.log(9 >> 1) //4
console.log(9 >> 2) //2
9
00000000000000000000000000001001 = 9
9 >> 1
00000000000000000000000000000100 = 4
9 >> 2
00000000000000000000000000000010 = 2
//-Ve Number
console.log(-9 >> 1) //-5
console.log(-9 >> 2) //-3
-9
11111111111111111111111111110111 = -9
-9 >> 1
11111111111111111111111111111011 = -5
-9 >> 2
11111111111111111111111111111101 = -3
The Unsigned right Shift (also known as zero-fill right shift) operator (>>>) shift the specified number of digits of the first operand to the right. The right operand specifies the number of digits to shift
a >>> b
Where
a: The first operand, whose digits to shift right
b: Number of digits to shift.
//+Ve Number
console.log(9 >>> 1) //4
console.log(9 >>> 2) //2
9
00000000000000000000000000001001 = 9
9 >> 1
00000000000000000000000000000100 = 4
9 >> 2
00000000000000000000000000000010 = 2
//-Ve Number
console.log(-9 >>> 1) //2147483643
console.log(-9 >>> 2) //1073741821
-9
11111111111111111111111111110111 = -9
-9 >> 1
01111111111111111111111111111011 = 2147483643
-9 >> 2
00111111111111111111111111111101 = 1073741821
Bitwise Assignment operators assign values to a JavaScript variable after performing the bitwise operation between the variable & the right operand.
Example
let y=9
y &=7
console.log(y) //1
//is same as
let y = 9
y = y & 7 ;
console.log(y) //1
List of Bitwise Assignment Operators
| Operator | Meaning |
|---|---|
| x &= y | x =x & y |
| x |=y | x =x | y |
| x ^=y | x =x ^ y |
| x <<=y | x =x << y |
| x >>=y | x =x >> y |
| x >>>=y | x =x >>> y |
Read More