JavaScript

JavaScript Bitwise Operators

Bitwise operators are used to perform numerical conversions in JavaScript. In JavaScript numbers are stored as 64 bits; however, all bitwise operations are worked on binary numbers with 32 bits. Therefore, JavaScript converts numbers into 32 bits before the implementation of bitwise operators. After the implementation of bitwise operators, the numbers are converted back into 64 bits.

The basic reason to use these operators is that these operators are much faster in math and parseInt equivalents. In this tutorial, we will learn about the bitwise operators in JavaScript and learn about the need and use of these operators. In this article, we describe the types of bitwise operators with examples.

Types of bitwise operators

All bitwise operators deal with their operands in a set of 32-bits; these bits are in the form of binary digits. However, the result of these bits is shown in decimal form. In the following table, we are defining multiple bitwise operators in JavaScript with code examples.

Representation of Operators

Name of Operators Symbol of Operator Example
AND Operator & a & b
OR Operator | a | b
NOT Operator ~ a ~ b
XOR Operator ^ a ^ b

Note: The maximum and minimum representable integer ranges are defined in bitwise operators such as

  • minimum representable integer range = -2147483648,
  • maximum representable integer range = 2147483647.

Let’s know about each operator one by one along with a couple of examples.

AND operator in JavaScript

There is two operands in binary form (0,1) in the AND operator. Its output is 0, if any of the operands is 0 and returns 1 if both operands are 1. The outputs of AND table are shown in the given table.

Operations of AND Operator

Operand 1 Operand 2 And Operation
1 1 1 & 1=1
1 0 1 & 0=0
0 1 0 & 1=0
0 0 0& 0=0

Let’s have a look at an example for further explanation of AND operator.

Example

There are two integers 12 and 25 for the implementation of AND operator.

At the first step, both integers are converted into bits. The bit conversion of 12 and 25 is:

12 = 00000000000000000000000000001100,

25 = 00000000000000000000000000011001.

After the conversion of integers, the AND operator is applied.

// First convert both integers in binary form,

12 = 01100

25 = 011001

00001100

& 00011001

--------

00001000 = 8 // (In decimal)

The preceding zeros are removed for the sake of simplicity. After the implementation of AND operator, the answer is 00001000, which is equal to 8 in integers.

Program of bitwise AND Operator in JavaScript

let a = 12;

let b = 25;

result = a & b;

console.log(result); // 8

OR operator in JavaScript:

There are two operands in binary form (0,1) in the OR operator. The output is 0, if both operands are 0s and output is 1 if any of one operand is 1. In the given table, all possible outputs of the OR operator are given:

Operations of OR Operator

Operand 1 Operand 2 And Operation
1 1 1 | 1 =1
1 0 1 | 0 =1
0 1 0 | 1 =1
0 0 0| 0 =0

Let’s have a look at an example for further explanation of OR operator.

Example

There are two integers 12 and 25 for the implementation of the OR operator.

First, we convert both integers into bits.The bit conversion of 12 and 25 is:

12 = 01100,

25 = 11001.

The preceding zeros are removed for the sake of simplicity.

// First convert both integers in binary form,

12 = 01100

25 = 11001

// Bitwise OR Operation of 12 and 25

00001100

| 00011001

--------

00011101 = 29 // (In decimal)

After the conversion of integers, the OR operator is applied the answer is 11101, which is equal to 29.

Program of bitwise OR Operator in JavaScript

// bitwise OR operator example

let a = 12;

let b = 25;

result = a | b;

console.log(result); // 29

XOR operator in JavaScript

There are two operands in binary form (0,1) in the XOR operator. It returns 0 as an output if both operands are the same and returns 1 as an output if both operands are different. In the given table all possible outputs of XOR operator are given:

Operations of XOR Operator

Operand 1 Operand 2 And Operation
1 1 1 ^ 1 = 0
1 0 1 ^ 0 = 1
0 1 0 ^ 1 = 1
0 0 0 ^ 0 = 0

Let’s have a look at an example for further explanation of XOR operator.

Example

There are two integers 12 and 25 for the implementation of the XOR operator.

First, we convert both integers into bits.The bit conversion of 12 and 25 is:

12 = 01100,

25 = 11001.
// First convert both integers in binary form,

12 = 01100

25 = 11001

// Bitwise XOR Operation of 12 and 25

01100

^ 11001

-----

10101 = 21 // (In decimal)

After the conversion of integers, the XOR operator is applied, the answer is 11101, which is equal to 21.

Program of bitwise XOR Operator in JavaScript

// bitwise XOR operator example

let a = 12;

let b = 25;

result = a ^ b;

console.log(result); // 21

NOT Operator in JavaScript

NOT operator is called as negation operator. It converts the bits 1 to 0 and 0 to 1. It is a unary operator among all the bitwise operators.

Operations of NOT Operator

Operand 1 NOT Operation
1 (~1) = 0
0 (~0) = 1

Let’s have a look at an example for further explanation of NOT operator.

Example

Since NOT operator is a unary operator; therefore, here we took only one operand (12) and applied Not operation on it.

First, convert the 12 into binary form:

12 = 00000000000000000000000000001100.

The NOT operator will convert all 0’s into the 1’s and changes all 1’s into the 0’s.

// First convert the integer in binary form,

12 = 00000000000000000000000000001100

// Bitwise Not Operation of 12

~ 00000000000000000000000000001100

---------------------------------

11111111111111111111111111110011 = -13 // (In decimal)

The answer will become 11111111111111111111111111110011, which is equal to -13 in integer form.

Program of bitwise NOT Operator in JavaScript

// bitwise NOT operator example

let b = 12;

result = ~b;

console.log(result); // -13

Conclusion

Bitwise operators are those which work on integers at binary level. There are total four bitwise operators in javascript: AND, OR, XOR, and NOT. This write-up explains all four bitwise types of operators and their working in JavaScript. We described all operators with their possible outputs in the form of tables. All operations are well explained by examples and codes.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.