Course
Bitwise Operators
JavaScript Tutorial
This JavaScript tutorial is crafted for beginners to introduce them to the basics and advanced concepts of JavaScript. By the end of this guide, you'll reach a proficiency level that sets the stage for further growth. Aimed at empowering you to progress towards becoming a world-class software developer, this tutorial paves the way for a successful career in web development and beyond.
Bitwise Operators
JavaScript Bitwise Operators
The bitwise operators in JavaScript perform operations on the integer values at the binary level. They are used to manipulate each bit of the integer values. Bitwise operators are similar to logical operators but they work on individual bits.
JavaScript bitwise operators works on 32-bits operands. In JavaScript, numbers are stored as 64-bit floating point number. JavaScript converts the numbers to 32-bit signed integer before performing the operation. After bitwise operation, it converts the result to 64-bits number.
There are seven bitwise operators in JavaScript. Following is the list of bitwise operators with description.
JavaScript Bitwise AND (&) Operator
The bitwise AND (&) operator performs AND operation on each pair of bits of its integer operands. After the operation, it returns a new integer value with the updated bits.
When bitwise AND operator is applied on a pair of bits, it returns 1 if both bits are 1, otherwise returns 0.
Following is the truth table for bitwise
AND
operationLet's understand bitwise AND operation taking an example of 4-bit operands.
Example
Let's perform bitwise
AND
(&) operation on 5 and 7. These numbers are represented as 32-bits integer.The resultant value of the OR operation of each bit of the 101 and 111 binary numbers is the same as below.
- 1 & 1 = 1
- 1 & 0 = 0
- 1 & 1 = 1
So, the resultant binary number is 111, which is equal to 7 in the decimal representation.
<html><body><div id="output"></div><script> const a = 5; const b = 7; document.getElementById("output").innerHTML = "a & b = " + (a & b);</script></body></html>
It will produce the following result
a & b = 5
JavaScript Bitwise OR (|) Operator
The bitwise
OR
(|) operator performs OR operation on each pair of bits of its integer operands. After the operation, it returns an integer value with the updated bits.When bitwise OR operator is applied on a pair of bits, it returns 1 if either of bits is 1, otherwise returns 0.
Following is the truth table for bitwise OR operation.
Let's understand bitwise OR operation taking an example of 4-bit operands.
Example
Let's perform bitwise
OR
(|) operation on 5 and 7. These numbers are represented as 32-bits integer.The resultant value of the OR operation of each bit of the 101 and 111 binary numbers is the same as below.
- 1 | 1 = 1
- 1 | 0 = 1
- 1 | 1 = 1
So, the resultant binary number is 111, which is equal to 7 in the decimal representation.
<html><body><div id="output"></div><script> const a = 5; const b = 7; document.getElementById("output").innerHTML = "a | b = " + (a | b);</script></body></html>
It will produce the following result
a | b = 7
JavaScript Bitwise XOR (^) Operator
The bitwise
XOR
(^) operator performs exclusive OR operation on each pair of bits of its integer operands. After the operation, it returns an integer value with the updated bits.When bitwise
XOR
operator is applied on a pair of bits, it returns 1 if both bits are different, otherwise returns 0.Following is the truth table for Bitwise
XOR
operation Example
Let's perform bitwise
XOR
(^) operation on 5 and 7.After performing the bitwise XOR operation of 101 and 111, the resultant binary number is given below.
- 1 ^ 1 = 0
- 1 ^ 0 = 1
- 1 ^ 1 = 0
So, the resultant binary number is 010, which is equal to 2.
<html><body><div id="output"></div><script> const a = 5; const b = 7; document.getElementById("output").innerHTML = "a ^ b = " + (a ^ b);</script></body></html>
It will produce the following output
a ^ b = 2
JavaScript Bitwise NOT (~) Operator
The bitwise
NOT
(~) operator performs the NOT
operation on each bit of the binary number. It is a unary operator that inverts each bit of the binary number and returns the 2’s complement to the binary number.Following is the truth table for the Bitwise
XOR
operation.Example
Let's perform bitwise
NOT
(~) operation.Try to execute the below code
<html><body><div id="output"></div><script> const a = 5; const b = 7; document.getElementById("output").innerHTML = "~a = " + (~a) + "<br>" + "~b = " + (~b)</script></body></html>
It will produce the following output
~a = -6~b = -8
Bitwise Left Shift (<<) Operator
The JavaScript bitwise left shift (
<<
) operator moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros from the right and left most bits are discarded.Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on.
Example
When you left shift 5 (101) by 1, a value becomes 10 (1010). When you perform the left shift operation by 2 places, the resultant value is 20 (10100).
The following JavaScript program demonstrates the bitwise left shift operation −
<html><body><div id="output"></div><script> const a = 5; document.getElementById("output").innerHTML = "a << 1 = " + (a << 1) + "<br>" + "a << 2 = " + (a << 2);</script></body></html>
It will produce the following output
a << 1 = 10a << 2 = 20
Bitwise Right Shift (>>) Operator
The bitwise right shift (
>>
) operator moves all the bits in its first operand to the right by the number of places specified in the second operand. It inserts copies of leftmost bit in from left and discard rightmost bits. In this way it preserves the sign of the number.In short, it removes the N last bits from the number. Here, N is a second operand. Right-shifting the binary number is equivalent to dividing the decimal number by 2.
Example
In the below example, when we perform the right shift operation on 101 for the first time, the value of a becomes equal to 010. After performing the right-shift operation for the second time, the resultant value is 001, equal to 1 in the decimal representation.
Try to execute the following program
<html><body><div id="output"></div><script> const a = 5; document.getElementById("output").innerHTML = "a >> 1 = " + (a >> 1) + "<br>" + "~a >> 1 = " + (~a >> 1);</script></body></html>
It will produce the following output
a >> 1 = 2~a >> 1 = -3
Bitwise Right Shift with Zero (>>>) Operator
The Right Shift with Zero (>>>) operator is very similar to the right shift operator. It always fills the left bits with zero without worrying about the sign of the bit.
Example
Here, the binary representation of 10 is 1010. When we perform the right shift with zero operation, it moves all bits 2 times in the right direction and inserts two 0 at the start. So, the resultant value will be 0010, equal to 1.
The following JavaScript program demonstrate the use of >>> operator.
<html><body><div id="output"></div><script> const a = 5; document.getElementById("output").innerHTML = "a >>> 1 = " + (a >>> 1);</script></body></html>
It will produce the following result
a >>> 1 = 2
You may try to use the different inputs with each operator and observe the output for more practices.