preface

CodeReview recently found that when some of the history of the project code, such as &, |, ^ ~, < <, > >, > > > symbols, I do not know much principle, through study the document.

Bitwise operators treat their operands as 32-bit sequences of bits (0s and 1s). That is, when a bit operation is performed on a piece of data in JS, it first converts the operands to 32-bit binary numbers, then manipulates the binary form of the number using bit-operated operators, and finally returns a result, which is still a standard JavaScript value.

Bitwise AND (AND)

Two binary numbers, their corresponding bits are 1, the result is 1, otherwise it is 0.

In javascript, bitwise and are represented by &. Such as:

     9 (base 10) = 00000000000000000000000000001001 (base 2)
    14 (base 10) = 00000000000000000000000000001110 (base 2)
                   --------------------------------
14 & 9 (base 10) = 00000000000000000000000000001000 (base 2) = 8 (base 10)
Copy the code

Use of bitwise operators – bitwise and

Bitwise OR (OR)

Two binary numbers that correspond to bits that have at least one 1 will result in a 1, or a 0 otherwise.

In javascript, bitwise or use the | said. Such as:

     9 (base 10) = 00000000000000000000000000001001 (base 2)
    14 (base 10) = 00000000000000000000000000001110 (base 2)
                   --------------------------------
14 | 9 (base 10) = 00000000000000000000000000001111 (base 2) = 15 (base 10)
Copy the code

XOR by bit

Two binary numbers that correspond to bits that have one and only one 1 will result in 1, or 0 otherwise.

In javascript, bitwise xor is denoted by ^. Such as:

     9 (base 10) = 00000000000000000000000000001001 (base 2)
    14 (base 10) = 00000000000000000000000000001110 (base 2)
                   --------------------------------
14 ^ 9 (base 10) = 00000000000000000000000000000111 (base 2) = 7 (base 10)
Copy the code

Use of bitwise operators – Bitwise xOR

Bitwise non (NOT)

Invert the number of corresponding bits, if it is 1, the result is 0; If it’s 0, it’s going to be 1.

In javascript, bitwise is not represented by ~. Such as:

   9 (base 10) = 00000000000000000000000000001001 (base 2)
                 --------------------------------
  ~9 (base 10) = 11111111111111111111111111110110 (base 2) = -10 (base 10)
Copy the code

Shift to the left

Moves the first right bit of a binary number to the left by a specified number of bits, with the right side supplemented by zeros.

In javascript, the left shift is denoted by <<. Such as:

       9 (base 10): 00000000000000000000000000001001 (base 2)
                    --------------------------------
  9 << 2 (base 10): 00000000000000000000000000100100 (base 2) = 36 (base 10) 
Copy the code

There’s a sign to the right

Moves the right bit of a binary number to the right by a specified number of bits. The right bit removed is abandoned. Copies the left most bit removed and fills the missing bit removed from the left. Since the new leftmost bit is always the same as before, the leftmost sign bit does not change.

In javascript, the symbol right is denoted by >>. Such as:

9 >> 2 = 2

     9 (base 10): 00000000000000000000000000001001 (base 2)
                  --------------------------------
9 >> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)
Copy the code

Minus 9 > 2 is minus 3, because the sign is preserved.


     -9 (base 10): 11111111111111111111111111110111 (base 2)
                   --------------------------------
-9 >> 2 (base 10): 11111111111111111111111111111101 (base 2) = -3 (base 10)
Copy the code

Unsigned right shift

Moves the first right bit of a binary number to the right by a specified number of bits. The bits moved out of the right are abandoned and the left is supplemented by zeros.

In javascript, unsigned right moves are denoted by >>>. Such as:

For positive numbers, 9 >>> 2 has the same value as 9 >> 2.

      9 (base 10): 00000000000000000000000000001001 (base 2)
                   --------------------------------
9 >>> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)
Copy the code

It’s not the same for negative numbers, because the left hand side is filled with zeros, and the minus sign is missing.

      -9 (base 10): 11111111111111111111111111110111 (base 2)
                    --------------------------------
-9 >>> 2 (base 10): 00111111111111111111111111111101 (base 2) = 1073741821 (base 10)
Copy the code

conclusion

After that, I’ll compile a classic example of how bitwise operators are often used in JavaScript.

reference

  • www.apiref.com/javascript-…