# JavaScript Bitwise Operators

In this JavaScript tutorial, you will learn all about JavaScript bitwise operators, types of bitwise operators with the appropriate examples.
In previous tutorials, we have seen approx all the JavaScript operators using examples.

## JavaScript Bitwise Operators

JavaScript Bitwise operators treat their operands as 32 bits binary digits ( zero or one ) and then perform actions on them.

Let’s see different-2 types of JavaScript bitwise operators.

### JavaScript Bitwise AND

JavaScript bitwise AND operator return 1 if the corresponding bit of both the operands is 1 otherwise returns 0.

One and Four bit example:

Let’s take an example to apply bitwise AND operator on two integers.

``````
let a = 12; // 01100
let b = 20; // 10100
result = a & b
console.log("Result is:- ", result);``````

Output will be:- 4 (0100)

### JavaScript Bitwise OR

JavaScript bitwise OR operator returns 1, if one of the bit is 1 otherwise returns 0.

One and Four bit example:

Let’s take an example to apply bitwise OR operator on two integers.

``````
let a = 12; // 01100
let b = 20; // 10100
result = a | b
console.log("Result is:- ", result);``````

Output will be:- 28 (00011100)

### JavaScript Bitwise XOR

JavaScript XOR ^ returns 1 if the corresponding bits are different otherwise it returns 0.

One and Four bit example:

Let’s take an example to apply bitwise XOR operator on two integers.

``````
let a = 30; // 011110
let b = 50; // 110010
result = a ^ b
console.log("Result is:- ", result);``````

Output will be:- 44 ( 101100)

### JavaScript Bitwise NOT

Bitwise Not operator invert the bit ( 0 becomes 1 and 1 becomes 0).

Example:

### JavaScript Bitwise left shift

In left shift operator, the left operand specify the number and right operand specify the number to be shifpted left.Zero bits are added to the right.

Example:

``````
let a = 5
let b = 1;
result = a << 1;
console.log("Result is:- ", result); // 1010``````

Output will be:- 10 ( 1010 )

### JavaScript Bitwise right shift

In the right-right operator, the right operand specifies the number and the right operand specifies the number to be shifted right. Zero bits are added to the left. Excess bits from the right side are discarded.

Example:

``````
let a = 5
let b = 1;
result = a >> 1;
console.log("Result is:- ", result); // 0010``````

Output will be:- 2 ( 0010 )

### JavaScript zero-fill right shift

JavaScript bitwise zero-fill right shift shifts the operand to the right by filling zero from the left and discard excess bits from the right.

Example:

``````
let a = 10
let b = 1;
result = a >> 1;
console.log("Result is:- ", result); // 0101``````

Output will be:- 5 ( 0101 )

## Convert Decimal to Binary

Here we will convert decimal number to binary number using toString() function.

Example:

``````function dec2bin(dec)
{
return (dec >>> 0).toString(2);
};

Output:- 10100

## Convert Binary to Decimal

Here we will convert binary to decimal number.

Example:

``````function bin2dec(bin)
{
return parseInt(bin, 2).toString(10);
};