JavaScript Bitwise Operators

JavaScript Bitwise Operators | With the aid of examples, you will discover JavaScript bitwise operators and their types in this tutorial. We will discuss what is bitwise operations in JavaScript and how they can help us while writing JavaScript code.

JavaScript Bitwise Operators

When performing operations, bitwise operators treat their operands as a collection of 32-bit binary digits (zeroes and ones). The outcome, however, is displayed as a decimal value. Before completing the action, JavaScript effectively converts 64-bit floating point numbers into 32-bit signed integers, and then it changes them back to 64-bit integers to output the result.

The mentioned bitwise operators are used in JavaScript:-

OperatorNameDescription
&ANDIf both bits are 1, the result is 1; otherwise 0.
|ORIf either bit is 1, the result is 1; otherwise 0.
^XORIf bits are different, the result is 1; otherwise 0.
~NOTIf the bit is 0, the result is 1; otherwise 0.
<<Zero-fill left shiftPushes zeros in from the right, and leftmost bits fall off.
>>Signed right shiftPushes copies of leftmost bit in from left, rightmost bit falls off (preserves sign).
>>>Zero fill right shiftPushes zeros in from left, and rightmost bits fall off.

Bitwise AND Operator in JavaScript

The output of bitwise AND is 1 if the corresponding bits of two operands are 1. If either bit of an operand is 0, the result of the corresponding bit is evaluated as 0. The Bitwise AND operator in JavaScript is denoted by &.

Bitwise AND truth table

PQP&Q
000
010
100
111

The bitwise AND operation of two integers 14 and 27,
14 = 00001110 (In Binary)
27 = 00011011 (In Binary)
Bit Operation of 14 and 27
00001110 & 00011011 = 00001010 = 10 (In decimal)

console.log(17 & 7);

Output:-

1

10001 = 17
00111 = 7
————–
00001 = 1

Note:- The & operator can be used to quickly check if a number is odd or even. The value of the expression (x 1) would be non-zero only if x is odd, otherwise, the value would be zero.

num = 9;
(num & 1) ? console.log("Odd") : console.log("Even");
num = 8;
(num & 1) ? console.log("Odd") : console.log("Even");

Output:-

Odd
Even

Bitwise OR Operator in JavaScript

The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C Programming, the bitwise OR operator is denoted by |.

Bitwise OR truth table

PQP | Q
000
011
101
111

14 = 00001110 (In Binary)
27 = 00011011 (In Binary)
Bitwise OR Operation of 14 and 27,
00001110 | 00011011 = 00011111 = 31 (In decimal)

console.log(19 | 7);

Output:-

23

10011 = 19
00111 = 7
—————
10111 = 23

Bitwise XOR Operator in JavaScript

The result of the bitwise XOR (exclusive OR) operator is 1 if the corresponding bits of two operands are opposite. It is denoted by ^.

Bitwise XOR truth table

PQP ^ Q
000
011
101
110

Bitwise XOR Operation of 14 and 27,
14 = 00001110 (In Binary)
27 = 00011011 (In Binary)
00001110 ^ 00011011 = 00010101 = 21 (In decimal)

console.log(17 ^ 7);

Output:-

22

10001 = 17
00111 = 7
—————
10110 = 22

Bitwise NOT Operator in JavaScript

Bitwise NOT truth table

P~ P
01
10

Since integers are kept in two’s complement, a ~ operation will alter the sign of the number and increase its exact value by one. To do this, invert the binary numbers.

Note:- The Bitwise complement of any number N is -(N+1).

console.log(~60);

Output:-

-61

The operator should be used carefully. The result of ~ operator on a small number can be a big number if the result is stored in an unsigned variable, And the result may be a negative number if the result is stored in a signed variable. (assuming that the negative numbers are stored in 2’s complement form where the leftmost bit is the sign bit).

Bitwise Zero fill left shift Operator in JavaScript

The left shift operator shifts all bits towards the left by a certain number of specified bits. It is denoted by <<.

  • 200 = 11001000
  • 200<<1 = 110010000 [Left shift by one bit]
  • 200<<4 = 110010000000 [Left shift by four bit]
  • 200<<0 = 11001000 (Shift by zero bit)

Shifting the bit of an integer by one position to the left is equivalent to multiplying by 2. So that shifting the bits by n positions to the left is equivalent to multiplication by 2n

console.log(3 << 2); 

Output:-

12

11 = 3
1100 = 12
3 * 22 = 3*4 = 12

Bitwise Zero fill signed right shift Operator in JavaScript

By forcing duplicates of the leftmost bit into the operand from the left, the >> operator maintains the operand’s sign.

Right shift operator, shifts all bits towards the right by a certain number of specified bits. It is denoted by >>.

  • 200 = 11001000
  • 200>>1 = 01100100 [Right shift by one bit]
  • 200>>2 = 00110010 [Right shift by two bits]
  • 200>>5 = 00000110 [Right shift by five bits]
  • 200>>8 = 00000000 [Right shift by eight bits]
  • 200>>0 = 11001000 (No Shift)

Shifting the bit of an integer by one position to the Right is equivalent to dividing by 2. So that shifting the bits by n positions to the right is equivalent to division by 2n

console.log(8 >> 0); 
console.log(8 >> 1); 
console.log(8 >> 2); 
console.log(8 >> 3); 
console.log(8 >> 4); 
console.log(8 >> 5); 

Output:-

8
4
2
1
0
0

8 = 1000
8 >> 0 = 1000 = 8
8 >> 1 = 0100 = 4
8 >> 2 = 0010 = 2
8 >> 3 = 0001 = 1
8 >> 4 = 0000 = 0
8 >> 5 = 0000 = 0

console.log(~8 >> 0);
console.log(~8 >> 1);
console.log(~8 >> 2);
console.log(~8 >> 3);
console.log(~8 >> 4);
console.log(~8 >> 5); 

Output:-

-9
-5
-3
-2
-1
-1

Bitwise Zero fill right shift Operator in JavaScript

The sign is not preserved by the >>> operator. The sign bit is pushed out of its leftmost location as a result of the leftward push of zeros.

console.log(20 >>> 8);

Output:-

0

console.log(-40 >>> 3);

Output:-

536870907

Also see:- JavaScript Get First Element of Array, How to Find the Longest String in an Array in JavaScript, JavaScript Remove Carriage Return and Line Feed, JavaScript Replace Carriage Return and Newline

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *