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>
   
   <p id="xyz"></p>
   
   <script>
      let x = 6, y = 3;
      document.getElementById("xyz").innerHTML = 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

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

JavaScript Online Test


« Previous Tutorial Next Tutorial »