Menu Close

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.

OperatorDescriptionExample
&Bitwise ANDx & y
|Bitwise ORx | y
^Bitwise XORx ^ y
~Bitwise NORx ~ y
<<Bitwise Left shiftx << y
>>Bitwise Righ shiftx >> y
>>>Bitwise Zero-fill right shiftx >>> y

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:

OperationResult
0 & 00
0 & 10
1 & 00
1 & 11
OperationResult
0000 & 00000000
0000 & 00010000
0000 & 00100000
1010 & 10101010

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:

OperationResult
0 | 00
0 | 11
1 | 01
1 | 11
OperationResult
0000 | 00000000
0000 | 00010001
0000 | 00100010
1110 | 10101110

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:

OperationResult
0 ^ 00
0 ^ 11
1 ^ 01
1 ^ 10
OperationResult
0000 ^ 00000000
0100 ^ 00010101
0000 ^ 00100010
1110 ^ 10100100

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:

OperationResult
500000000000000000000000000000101
~511111111111111111111111111111010

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);
};

alert(dec2bin(20));

Output:- 10100

Convert Binary to Decimal

Here we will convert binary to decimal number.

Example:

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

Output:- 22

Conclusion

In this article, you have learned all about JavaScript Bitwise operators with the help of examples. JavaScript stores all the numbers as 64-bit floating-point numbers, but all the bitwise operations performed on 32 bits binary numbers.

If this article ( JavaScript bitwise operators ) helpful for you, please share and keep visiting for further JavaScript tutorials.

JavaScript Array Methods

Related Posts