Thursday, December 26, 2024
Google search engine
HomeLanguagesJavaJavaScript Bitwise Operators

JavaScript Bitwise Operators

JavaScript uses 32 bits Bitwise operands. A number is stored as a 64-bit floating-point number but the bit-wise operation is performed on a 32-bit binary number i.e. to perform a bit-operation JavaScript converts the number into a 32-bit binary number (signed) and performs the operation and converts back the result to a 64-bit number.

Below is a list of bitwise operators:

OPERATOR NAME USAGE DESCRIPTION
Bitwise AND(&) a&b Returns true if both operands are true
Bitwise OR(|) a|b Returns true even if one operand is true
Biwise XOR(^) a^b Returns true if both operands are different
Bitwise NOT(~) a~b Flips the value of the operand
Bitwise Left Shift(<<) a<<b Shifts the bit toward the left
Bitwise Right Shift(>>) a>>b Shifts the bit towards the right
Zero Fill Right Shift(>>>) a>>>b Shifts the bit towards the right but adds 0 from left

Below are a few bit-wise logical operators used in JavaScript:

  • Bitwise AND ( & ): It is a binary operator i.e. accepts two operands. Bit-wise AND (&) returns 1 if both the bits are set ( i.e 1) and 0 in any other case.
A B OUTPUT ( A & B )
0 0 0
0 1 0
1 0 0
1 1 1
  • Bitwise OR ( | ): It is a binary operator i.e. accepts two operands. Bit-wise OR ( | ) returns 1 if any of the operands is set (i.e. 1) and 0 in any other case.
A B OUTPUT ( A | B )
0 0 0
0 1 1
1 0 1
1 1 1
  • Bitwise XOR ( ^ ): It is a binary operator i.e. accepts two operands. Bit-wise XOR ( ^ ) returns 1 if both the operands are different and 0 in any other case.
A B OUTPUT ( A ^ B )
0 0 0
0 1 1
1 0 1
1 1 0
  • Bitwise NOT ( ~ ): It is a unary operator i.e. accepts single operands. Bit-wise NOT ( ~ ) flips the bits i.e 0 becomes 1 and 1 becomes 0.
A OUTPUT ( ~A )
0 1
1 0

Below is an example of the JavaScript Bitwise Operators:

Example: 

JavaScript




let a = 4;
let b = 1;
 
console.log("A & B = " + (a & b));
console.log("A | B = " + (a | b));
console.log("~A = " + (~a));


Output:

A & B = 0
A | B = 5
~A = -5

Below are a few bit-wise shift operators used in JavaScript:

  • Left Shift ( << ): It’s a binary operator i.e. it accepts two operands. The first operator specifies the number and the second operator specifies the number of bits to shift. Each bit is shifted towards the left and 0 bits are added from the right. The excess bits from the left are discarded.
A 6 ( 00000000000000000000000000000110 )
B 1 ( 00000000000000000000000000000001 )
OUTPUT ( A << B ) 12 ( 00000000000000000000000000001100 )
  • Sign Propagating Right Shift ( >> ): It’s a binary operator i.e. it accepts two operands. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bit is shifted towards the right, the overflowing bits are discarded. This is Sign Propagating as the bits are added from the left depending upon the sign of the number (i.e. 0 if positive and 1 if negative )
A 6 ( 00000000000000000000000000000110 )
B 1 ( 00000000000000000000000000000001 )
OUTPUT ( A >> B ) 3 ( 00000000000000000000000000000011 )
  • Zero Fill Right Shift ( >>> ): It’s a binary operator i.e. it accepts two operands. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bit is shifted towards the right, the overflowing bits are discarded. 0 bit is added from the left so its zero fill right shift.
A 6 ( 00000000000000000000000000000110 )
B 1 ( 00000000000000000000000000000001 )
OUTPUT ( A >>> B ) 3 ( 00000000000000000000000000000011 )

Example: Below is the implementation of the above-described operators.

JavaScript




let a = 6;
let b = 1;
 
// AND Operation
console.log("A & B = " + (a & b));
 
// OR operation
console.log("A | B = " + (a | b));
 
// NOT operation
console.log("~A = " + (~a));
 
// Sign Propagating Right Shift
console.log("A >> B = " + (a >> b));
 
// Zero Fill Right Shift
console.log("A >>> B = " + (a >>> b));
 
// Left Shift
console.log("A << B = " + (a << b));


Output:

A & B = 0
A | B = 7
~A = -7
A >> B = 3
A >>> B = 3
A << B = 12

Supported Browsers: The browsers supported by all JavaScript Bitwise operators are listed below.

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Apple Safari
  • Opera

We have a complete list of Javascript operators, to check those please go through this Javascript Operators Complete Reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.

RELATED ARTICLES

Most Popular

Recent Comments