Bitwise Operators in JavaScript

Bitwise operators in JavaScript are used to perform bitwise (bit-wise or bit-by-bit) operations. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <script>
      let x = 6, y = 3;
      document.write(x & y);
   </script>
   
</body>
</html>
Output

Note - Bit operators works on 32-bits number. Bitwise operations goes on bit patterns (binary numerals), involves individual bits manipulation.

In above example, the binary equivalent of 6 is 110 (or 0000 0000 0000 0000 0000 0000 0000 0110 in 32-bit number). Whereas the binary equivalent of 3 is 11 (or 0000 0000 0000 0000 0000 0000 0000 0011 in 32-bit number). Therefore:

    1 1 0
&   0 1 1
----------
    0 1 0

The 010 or 10 is equal to 2 in decimal. That is, in 32-bit numbers:

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 (6)
&   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 (3)
-----------------------------------------------------------------------
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 (2)

List of All Bitwise Operators in JavaScript

  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (^)
  • Bitwise NOT (˜)
  • Bitwise Right Shift (>>)
  • Bitwise Left Shift (<<)
  • Bitwise Unsigned Right Shift (>>>)

To learn about Bitwise operators with example in detail, refer to its separate tutorial.

JavaScript Online Test


« Previous Tutorial Next Tutorial »