# 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:

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.
• 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.
• 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.
• 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.

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.
• 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 )
• 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.

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.

• 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.

Previous
Next