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.
Headings of Contents
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.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | x & y |
| | Bitwise OR | x | y |
^ | Bitwise XOR | x ^ y |
~ | Bitwise NOR | x ~ y |
<< | Bitwise Left shift | x << y |
>> | Bitwise Righ shift | x >> y |
>>> | Bitwise Zero-fill right shift | x >>> 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:
Operation | Result |
---|---|
0 & 0 | 0 |
0 & 1 | 0 |
1 & 0 | 0 |
1 & 1 | 1 |
Operation | Result |
---|---|
0000 & 0000 | 0000 |
0000 & 0001 | 0000 |
0000 & 0010 | 0000 |
1010 & 1010 | 1010 |
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:
Operation | Result |
---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Operation | Result |
---|---|
0000 | 0000 | 0000 |
0000 | 0001 | 0001 |
0000 | 0010 | 0010 |
1110 | 1010 | 1110 |
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:
Operation | Result |
---|---|
0 ^ 0 | 0 |
0 ^ 1 | 1 |
1 ^ 0 | 1 |
1 ^ 1 | 0 |
Operation | Result |
---|---|
0000 ^ 0000 | 0000 |
0100 ^ 0001 | 0101 |
0000 ^ 0010 | 0010 |
1110 ^ 1010 | 0100 |
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:
Operation | Result |
---|---|
5 | 00000000000000000000000000000101 |
~5 | 11111111111111111111111111111010 |
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.