Today I read “You Don’t Know JavaScript” and read a chapter on cast casting, which mentioned the bit operators in JavaScript. I happened to have touched some relevant principles in analog circuit and digital circuit class, so I wrote this article to sort them out

After a search, basically these are:

  • Bitwise and operators:&
  • Bitwise or operator:|
  • Bitwise xOR operators:^
  • Bitwise inverse operator:~
  • Right shift operator:>>
  • Left-shift operator:<<

1. Bitwise and operators:&

  • Property: Binary operator
  • Usage:6 & 8 (operand 1 & operand 2)
  • Rule: 0 if there are 0, 1 if there are double 1
  • Principle: First convert 6 and 8 into binary numbers, respectively0000, 0110,and0000, 1000,
// According to the rules:
/ / 11 - > 1
// 10 00 -> 0
    0000 0110
    0000 1000
-------------------
    0000 0000
Copy the code

The result is0000, 0000,To go back to decimal0


2. Bitwise or operator:|

  • Property: Binary operator
  • Usage:1 | 1 | 2 (operand operand 2)
  • Rule: 1 if there is 1, 0 if there is double 0
  • Principle: First convert 1 and 2 into binary numbers, respectively0000, 0001,and0000, 0010,
// According to the rules:
// 10 11 -> 1
/ / 00 - > 0
    0000 0001
    0000 0010
-------------------
    0000 0011
Copy the code

The result is0000, 0011,Is converted to a decimal number3


3. Bitwise XOR operators:^

  • Property: Binary operator
  • Usage:3 ^ 6 (operands 1 ^ 2)
  • Rule: 10 = 1, 00, 11 = 0 (same bit = 0, different bit = 1)
  • Principle: Convert 3 and 6 to binary numbers, respectively0000, 0011,and0000, 0110,
// According to the rules:
/ / 10 - > 1
// 00 11 -> 0
    0000 0011
    0000 0110
-------------------
    0000 0101
Copy the code

The result is0000, 0101,Is converted to a decimal number5


4. Reverse operator by bit:~

  • Property: unary operator
  • Usage:~1 (~ operand)
  • The rules
    • Positives are reversed in bitwise (see 4.1)
    • Negative numbers are reversed in bitwise (see 4.2)

4.1 Positive numbers are reversed in bitwise

Here are the rules:

  1. The decimal positive integer is first converted into a binary number1As an example

1 -> 0000 0001 2. Then invert the binary source code, and the result is 1111 1110

    0000 0001
-------------------
    1111 1110
Copy the code
  1. At this time1111, 1110,The first11 is negative, 0 is positive, this sign bit will not participate in the operation, that is to say, we will take the last 7 bits except the first bit for operation, here is:111, 1110,When the operand is inverted and then +1, we get the final result:
// take the reverse operation
    111 1110
-------------------
    000 0001
+  0000 0001  // Decimal 1 is converted to binary (0000 0001)
-------------------
    000 0010
Copy the code
  1. Splicing symbol bits and operation results:1000, 0010,

The first sign bit is 1, which means that this is a negative number, and the binary number that follows is 2, so it’s 12 -

4.2 Negative numbers are reversed by bit

Here are the rules:

  1. The decimal positive integer is first converted into a binary number2 -As an example

2 -> 1000 0010 The first sign bit is reserved, and the next seven bits are reversed and +1

    000 0010
-------------------
    111 1101
+  0000 0001
-------------------
    111 1110
Copy the code
  1. Merge sign bit and operand and negate whole
    1111 1110
-------------------
    0000 0001
Copy the code
  1. Convert the source code to a decimal number:0000, 0001, -> 1


5. Right shift operator:>>

  • Property: Binary operator
  • Usage:3 >> 1 (operands >> right shift number)
  • Rule: sign bit unchanged, move right bit, left fill 0
  • How it works: First convert 3 to binary0000, 0011,
    0  |000 0011|
-------|--------|----
    0  |000 0001|1	// The last 1 is squeezed out by the right shift, and a 0 is added to the left, because the number of bits in binary is fixed
Copy the code

So the final result is:0000, 0001,, converted to decimal1


6. Left shift operator:<<

  • Property: Binary operator
  • Usage:1 << 2 (operands << left shift number)
  • Rule: sign bit unchanged, move left bit, add 0 right
  • Principle: The basic principle is the same as the right shift operator, which first converts 1 to binary0000, 0001,
	0   |000 0001|
------------|--------|--
	0 00|000 0100|	// Move 2 bits to the left, squeezing out 2 zeros on the left and adding 2 zeros on the right
Copy the code

So the final result is:0000, 0100,, is converted to a decimal number4



If you follow through here, you’ll see: We can use it1 << xTo findTwo to the x, thanMath.pow(2, x)Make it more concise.


summary

Although usually basically will not contact the level of operation, but the understanding is still to understand, often see oN the Internet JavaScript strange technology clever use in place operator, computer extensive and profound, the road is still long, keep forward.