JavaScript: Bitwise Operators

Although not a commonly used operator, the bitwise operator is essential to keep in mind that this operator handles the operand inside the variable as its 32-bit machine language. Although it's operating under binary digits, the output is displayed in decimal.

1. Bitwise AND (&)

Bitwise AND and && from the logical operator are two separate things that cannot be confused with one another. Here the operator gives out 1 if both the operand bits are 1. Else, it gives out 0.

So basically.

0 and 0 is equal to 0
0 and 1 is equal to 0
1 and 0 is equal to 0
1 and 1 is equal to 1

Example: 

let x = 15;
let y = 26;

finalOut = x & y
console.log(finalOut)

Output: 10

00001111
00011010
--------
00001010 => 10
  • The binary value of 15 is 00001111.
  • And 26 is 00011010.
  • So 00001010 is 10 in decimal.
  • Turns out the final output, as discussed, has given out the decimal value.

2. Bitwise OR (|)

If either of both sides of an operand consists of 1, the bit gives out 1. Else it gives out 0.

0 and 0 is equal to 0
0 and 1 is equal to 1
1 and 0 is equal to 1
1 and 1 is equal to 1

Example: 

let x = 15;
let y = 26;

finalOut = x | y
console.log(finalOut)

Output: 31

00001111
00011010
--------
00011111  => 31

3. Bitwise XOR (^)

This one gives out 0 if both operand's bits are the same and gives out 1 if it is different.

0 and 0 is equal to 0
0 and 1 is equal to 1
1 and 0 is equal to 1
1 and 1 is equal to 0

Example: 

let x = 15;
let y = 26;

finalOut = x ^ y
console.log(finalOut)

Output: 21

00001111
00011010
--------
00010101  => 21

4. Bitwise NOT (~)

The NOT Operator inverts the operand bits opposite to its corresponding bit.

00001111 in NOT turns to 11110000

Example:

let x = 15;

finalOut = ~x
console.log(finalOut)

Output: -16

  • While converting 00001111 to 11110000, it gives out 240.
  • The value is then computed to decimal signed 2's complement.
  • 2's complement is nothing, but after the inversion, it gets added with 1.
11110000
       1
---------
11110001 => -16

5. Bitwise Left Shift (<<)

  • The left shift operator shifts the left operand specified from right to left. It adds zeros. The number of zeros depends upon the right operand.
  • So 15 is 00001111 shifting left with 26 gives out 00001111, 00000000000000000000000000 which is in decimal value 1006632960.

Example: 

let x = 15;
let y = 26;
finalOut = x << y
console.log(finalOut)

Output: 1006632960

6. Bitwise Right Shift (>>)

  • In the right shift, the operator removes the last bits of the first operand from the right side.
  • So again, 15 is 00001111. Doing a right shift with, say 2, removes the last two bits 000011, which in decimal value is 3.
let x = 15;
let y = 2;
finalOut = x >> y
console.log(finalOut)

Output: 3

So likewise >>> and >> are both similar to what they do by pushing from left to right and removing the last right side bits by the specified operand.

Fri, 10/01/2021 - 08:57

Authored by

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.
Tags