JavaScript Bitwise Operators

09 Sep, 2020
Below is the example of the JavaScript Bitwise Operators.Example:
<script> var a = 4; var b = 1;   document.write("A & B = " + (a & b) + '<br>');   document.write("A | B = " + (a | b) + '<br>');   document.write("~A = " + (~a) + '<br>'); </script>
Output:
A & B = 0
A | B = 5
~A = -5
Like C, C++, Java, Python, and various other languages, JavaScript also supports bit-wise operations. In JavaScript, 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 perform the operation and convert back the result to a 64-bit number.
Below are few bit-wise logical operators used in JavaScript.
Bit-wise 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.ABOUTPUT ( A & B )000010100111
Bit-Wise OR ( | ) : Its is a binary operator i.e. accepts two operands. Bit-wise OR ( | ) returns 1 if any of the operand is set (i.e. 1) and 0 in any other case.ABOUTPUT ( A | B )000011101111
Bit-Wise XOR ( ^ ) : Its 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.ABOUTPUT ( A ^ B )000011101110
Bit-Wise NOT ( ~ ) : Its is a unary operator i.e. accepts single operands. Bit-wise NOT ( ~ ) flips the bits i.e 0 becomes 1 and 1 becomes 0.AOUTPUT ( ~A )0110Below are few bit-wise shift operators used in JavaScript.Left Shift ( << ): Its a binary operator i.e. it accepts two operand. The first operator specifies the number and the second operator specifies the number of bits to shift. Each bits are shifted towards left and 0 bits are added from the right. The excess bits from the left are discarded.A6 ( 00000000000000000000000000000110 )B1 ( 00000000000000000000000000000001 )OUTPUT ( A << B )12 ( 00000000000000000000000000001100 )Sign Propagating Right Shift ( >> ) : Its a binary operator i.e. it accepts two operand. 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 which are added from the left depends upon the sign of the number (i.e. 0 if positive and 1 if negative )A6 ( 00000000000000000000000000000110 )B1 ( 00000000000000000000000000000001 )OUTPUT ( A >> B )3 ( 00000000000000000000000000000011 )Zero fill Right Shift ( >>> ) : Its a binary operator i.e. it accepts two operand. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bits is shifted towards right, the overflowing bits are discarded. 0 bit is added from the left so its zero fill right shift.A6 ( 00000000000000000000000000000110 )B1 ( 00000000000000000000000000000001 )OUTPUT ( A >>> B )3 ( 00000000000000000000000000000011 )Below is the implementation of the above described operators.<script>var a = 6;var b = 1;  // AND Operationdocument.write("A & B = " + (a & b) + '<br>');    // OR operationdocument.write("A | B = " + (a | b) + '<br>');  // NOT operationdocument.write("~A = " + (~a) + '<br>');    // Sign Propagating Right Shiftdocument.write("A >> B = " + (a >> b) + '<br>');    // Zero Fill Right Shiftdocument.write("A >>> B = " + (a >>> b) + '<br>');    // Left Shiftdocument.write("A << B = " + (a << b) + '<br>'); </script>                            Output:A & B = 0
A | B = 7
~A = -7
A >> B = 3
A >>> B = 3
A << B = 12Supported Browsers:Google ChromeInternet ExplorerFirefoxApple SafariOperaMy Personal Notes
arrow_drop_upSave
Below are few bit-wise shift operators used in JavaScript.
Left Shift ( << ): Its a binary operator i.e. it accepts two operand. The first operator specifies the number and the second operator specifies the number of bits to shift. Each bits are shifted towards left and 0 bits are added from the right. The excess bits from the left are discarded.A6 ( 00000000000000000000000000000110 )B1 ( 00000000000000000000000000000001 )OUTPUT ( A << B )12 ( 00000000000000000000000000001100 )Sign Propagating Right Shift ( >> ) : Its a binary operator i.e. it accepts two operand. 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 which are added from the left depends upon the sign of the number (i.e. 0 if positive and 1 if negative )A6 ( 00000000000000000000000000000110 )B1 ( 00000000000000000000000000000001 )OUTPUT ( A >> B )3 ( 00000000000000000000000000000011 )Zero fill Right Shift ( >>> ) : Its a binary operator i.e. it accepts two operand. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bits is shifted towards right, the overflowing bits are discarded. 0 bit is added from the left so its zero fill right shift.A6 ( 00000000000000000000000000000110 )B1 ( 00000000000000000000000000000001 )OUTPUT ( A >>> B )3 ( 00000000000000000000000000000011 )Below is the implementation of the above described operators.<script>var a = 6;var b = 1;  // AND Operationdocument.write("A & B = " + (a & b) + '<br>');    // OR operationdocument.write("A | B = " + (a | b) + '<br>');  // NOT operationdocument.write("~A = " + (~a) + '<br>');    // Sign Propagating Right Shiftdocument.write("A >> B = " + (a >> b) + '<br>');    // Zero Fill Right Shiftdocument.write("A >>> B = " + (a >>> b) + '<br>');    // Left Shiftdocument.write("A << B = " + (a << b) + '<br>'); </script>                            Output:A & B = 0
A | B = 7
~A = -7
A >> B = 3
A >>> B = 3
A << B = 12Supported Browsers:Google ChromeInternet ExplorerFirefoxApple SafariOperaMy Personal Notes
arrow_drop_upSave
Left Shift ( << ): Its a binary operator i.e. it accepts two operand. The first operator specifies the number and the second operator specifies the number of bits to shift. Each bits are shifted towards left and 0 bits are added from the right. The excess bits from the left are discarded.A6 ( 00000000000000000000000000000110 )B1 ( 00000000000000000000000000000001 )OUTPUT ( A << B )12 ( 00000000000000000000000000001100 )
Sign Propagating Right Shift ( >> ) : Its a binary operator i.e. it accepts two operand. 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 which are added from the left depends upon the sign of the number (i.e. 0 if positive and 1 if negative )A6 ( 00000000000000000000000000000110 )B1 ( 00000000000000000000000000000001 )OUTPUT ( A >> B )3 ( 00000000000000000000000000000011 )
Zero fill Right Shift ( >>> ) : Its a binary operator i.e. it accepts two operand. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bits is shifted towards right, the overflowing bits are discarded. 0 bit is added from the left so its zero fill right shift.A6 ( 00000000000000000000000000000110 )B1 ( 00000000000000000000000000000001 )OUTPUT ( A >>> B )3 ( 00000000000000000000000000000011 )Below is the implementation of the above described operators.<script>var a = 6;var b = 1;  // AND Operationdocument.write("A & B = " + (a & b) + '<br>');    // OR operationdocument.write("A | B = " + (a | b) + '<br>');  // NOT operationdocument.write("~A = " + (~a) + '<br>');    // Sign Propagating Right Shiftdocument.write("A >> B = " + (a >> b) + '<br>');    // Zero Fill Right Shiftdocument.write("A >>> B = " + (a >>> b) + '<br>');    // Left Shiftdocument.write("A << B = " + (a << b) + '<br>'); </script>                            Output:A & B = 0
A | B = 7
~A = -7
A >> B = 3
A >>> B = 3
A << B = 12Supported Browsers:Google ChromeInternet ExplorerFirefoxApple SafariOperaMy Personal Notes
arrow_drop_upSave
Below is the implementation of the above described operators.
<script>var a = 6;var b = 1;  // AND Operationdocument.write("A & B = " + (a & b) + '<br>');    // OR operationdocument.write("A | B = " + (a | b) + '<br>');  // NOT operationdocument.write("~A = " + (~a) + '<br>');    // Sign Propagating Right Shiftdocument.write("A >> B = " + (a >> b) + '<br>');    // Zero Fill Right Shiftdocument.write("A >>> B = " + (a >>> b) + '<br>');    // Left Shiftdocument.write("A << B = " + (a << b) + '<br>'); </script>                            
Output:
A & B = 0
A | B = 7
~A = -7
A >> B = 3
A >>> B = 3
A << B = 12
Supported Browsers:
Google Chrome
Internet Explorer
Firefox
Apple Safari
Opera
javascript-operators
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.