JS Bitwise Operators

JavaScript

In this tutorial we will learn about JavaScript bitwise operators.

We use the bitwise operator to work with bits (0s and 1s).

Click here for Boolean Algebra tutorial.
Click here for Logic Gates tutorial.

Following is a list of bitwise operators.

OperatorSymbolExample
AND&x & y
OR|x | y
NOT~~x
XOR^x ^ y
Left shift<<2 << 1
Sign propagating Right shift>>2 >> 1
Right shift with fill zero>>>2 >> 1

Click here for Decimal to Binary conversion tutorial.
Click here for Binary to Decimal conversion tutorial.

32 bits signed integer

Operands of a bitwise operator is converted into 32 bit signed integer in two's complement.

The last bit of the two's complement is the sign bit. It is 0 for positive numbers and 1 for negative numbers.

Numbers we can represent using 32 bits is from -2147483648 to 2147483647.

Formula: -2(32-1) to 2(32-1) - 1

How to convert decimal integer (base 10) into binary (base 2)?

Click here for Decimal to Binary conversion tutorial.

Consider we have a number 4 in decimal number system i.e., base 10. So, in binary number system it will be denoted as (100)2 which is in base 2.

4 (base 10) = 00000000 00000000 00000000 00000100 (base 2)

How to convert a decimal number in two's complement?

Steps to convert a decimal number in two's complement.

  1. Convert the decimal number into binary
  2. Invert the binary bits (1's complement)
  3. Add 1 to the 1's complement to get the 2's complement

Convert 4 into 2's complement.

Step 1: Decimal to Binary
----------------------------
4 (base 10) = 00000000 00000000 00000000 00000100 (base 2)


Step 2: Compute 1's complement by inverting bits
---------------------------------------------------
    00000000 00000000 00000000 00000100
    11111111 11111111 11111111 11111011 (1's complement)


Step 3: Compute 2's complement from 1's complement by adding 1
-----------------------------------------------------------------
    11111111 11111111 11111111 11111011 (1's complement)
                                     +1
   -------------------------------------
    11111111 11111111 11111111 11111100 (2's complement)


Bitwise AND &

This will give 1 only if value of x and y are both 1.

Bitwise AND
      4 (base 10) = 00000000 00000000 00000000 00000100 (base 2)
      6 (base 10) = 00000000 00000000 00000000 00000110 (base 2)
 ----------------------------------------------------------------
  4 & 6 (base 10) = 00000000 00000000 00000000 00000100 (base 2)
                  = 4 (base 10)

JS Code

console.log(4 & 6);	//this will print 4

Bitwise OR |

This will give 1 if value of x or y is 1.

Bitwise OR
      4 (base 10) = 00000000 00000000 00000000 00000100 (base 2)
      6 (base 10) = 00000000 00000000 00000000 00000110 (base 2)
 ----------------------------------------------------------------
  4 | 6 (base 10) = 00000000 00000000 00000000 00000110 (base 2)
                  = 6 (base 10)

JS Code

console.log(4 | 6);	//this will print 6

Bitwise NOT ~

This will invert the bit.

Bitwise NOT
      4 (base 10) = 00000000 00000000 00000000 00000100 (base 2)
 ----------------------------------------------------------------
     ~4 (base 10) = 11111111 11111111 11111111 11111011 (base 2)
                  = -5 (base 10)

Bitwise NOT of any number N yields -(N + 1).
Example: ~10 = -11 (base 10)

JS Code

console.log(~4);	//this will print -5

Bitwise XOR ^

This will give 1 if value of x and y are different.

Bitwise XOR
      4 (base 10) = 00000000 00000000 00000000 00000100 (base 2)
      6 (base 10) = 00000000 00000000 00000000 00000110 (base 2)
 ----------------------------------------------------------------
  4 ^ 6 (base 10) = 00000000 00000000 00000000 00000010 (base 2)
                  = 2 (base 10)

JS Code

console.log(4 ^ 6);	//this will print 2

Left shift <<

This will shift the bits of the first operand to specified number of bits to the left.

Left shift
      4 (base 10) = 00000000 00000000 00000000 00000100 (base 2)
 ----------------------------------------------------------------
 4 << 2 (base 10) = 00000000 00000000 00000000 00010000 (base 2)
                  = 16 (base 10)

JS Code

console.log(4 << 2);	//this will print 16

Sign propagating right shift >>

This will shift the bits of the first operand to specified number of bits to the right.

Sign propagating right shift
      8 (base 10) = 00000000 00000000 00000000 00001000 (base 2)
 ----------------------------------------------------------------
 8 >> 2 (base 10) = 00000000 00000000 00000000 00000010 (base 2)
                  = 2 (base 10)

JS Code

console.log(8 >> 2);	//this will print 2
Sign propagating right shift
     -4 (base 10) = 11111111 11111111 11111111 11111100 (base 2)
-----------------------------------------------------------------
-4 >> 1 (base 10) = 11111111 11111111 11111111 11111110 (base 2)
                  = -2 (base 10)

JS Code

console.log(-4 >> 1);	//this will print -2

Right shift with fill zero >>>

This will shift the bits of the first operand to specified number of bits to the right and will add leading zeros.

Right shift with fill zero
       8 (base 10) = 00000000 00000000 00000000 00001000 (base 2)
 ----------------------------------------------------------------
 8 >>> 2 (base 10) = 00000000 00000000 00000000 00000010 (base 2)
                   = 2 (base 10)

JS Code

console.log(8 >>> 2);	//this will print 2
Right shift with fill zero
      -4 (base 10) = 11111111 11111111 11111111 11111100 (base 2)
-----------------------------------------------------------------
-4 >>> 1 (base 10) = 01111111 11111111 11111111 11111110 (base 2)
                   = 2147483646 (base 10)

JS Code

console.log(-4 >>> 1);	//this will print 2147483646

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT