Bit operation has been not quite understand, so find a time to learn this, if there is a bad comment please contact me to correct

What is a bit operation?


Bitwise operations are performed at the base of the number (the 32 digits that represent the number). Because bit operation is low-level operation, so the speed is often the fastest (relative to other operations such as addition, subtraction, multiplication and division), and with bit operation sometimes we can also achieve simpler program logic, the disadvantage is very not intuitive, many occasions can not be used

The specific computer knowledge, we will not go into detail here, as long as you know that a point operation is calculated in binary, the so-called bit is 0110 one digit

Example:

decimal binary Js displays binary
1 0001 1
2 0010 10
5 0101 101
10 1010 1010

Note: the binary will be filled with 1, so 2 will be equal to 0010,js print binary, the default will remove the previous complement 0, such as 0001, will become 1,

Let’s get down to business

  1. Or operation = a | b

To each one are compared, and the or position of a and b with an arbitrary binary digits is 1, the result is 1, which is a | b is 1, you can also understand for | |

Example: 9 | 3 = 11

The decimal system = The fourth The third The second The first
9 = 1 0 0 1
3 = 0 0 1 1
11 = 1 0 1 1

Compare the first ‘1’ of 9 with the first ‘1’ of 3, satisfy that one of them has 1, so the result of the calculation out of the first is equal to 1, complement zero is: 0001, the other bits are the same.

Second place: 1 third place: 0 fourth place: 1 united: 1011

Use: 1 rounded

let a = 1.5
console.log(a | 0) / / 1
// Mechanism: 1.5 => 0001
/ / 0 = > 0000
// 0001 input is 1
Copy the code

  1. And operation = a &b

If a AND b are both 1, the result is 1. A & b = 1

Example: 9 & 3 = 1

The decimal system = The fourth The third The second The first
9 = 1 0 0 1
3 = 0 0 1 1
1 = 0 0 0 1

Compare the first ‘1’ of 9 with the first ‘1’ of 3, both are 1, calculate the result: the first 1, complement zero namely: 0001, other same principle.

Second place: 0 third place: 0 fourth place: 0, united: 0001

One use: judge odd and even

The binary digit of an odd number must end with 1, so any odd number &1 must be equal to 1

const isOdd = (num) = > num & 1= = =1;
isOdd(5)
// Principle: 1 => 0101
/ / 0 = > 0001
// 0001 input is 1
Copy the code
  1. Xor is equal to a to the b

    What is xOR: Are the two numbers different? 1 ^ 1 = 0 because there is no difference,

    1 to the 0 is 1

If a and b are not equal, the result is 1. 1 ^ 0 = 1 = =

Example: 9 ^ 3 = 1

The decimal system = The fourth The third The second The first
9 = 1 0 0 1
3 = 0 0 1 1
10 = 1 0 1 0

There is no difference between the first ‘1’ of 9 and the first ‘1’ of 3. == 1 =false, calculate result: 0, add zero namely: 0000, other bits same principle.

Second place: 1 third place: 0 fourth place: 1 united: 1010

Use: Exchange two variables (not three)

let a = 5,
    b = 6;

a = a ^ b; 
b = a ^ b;
a = a ^ b; // The last step is to return to the original value
Copy the code
  1. Bitwise, not = ~ A

Performs a NOT operation on each bit. The result is the reverse of a.

Ps: The bitwise non-operation on any value x results in -(x + 1). For example, ~5 results in -6:

Example: ~5 = -6

Application Scenarios:

  1. integer
~ ~ (-5.88) / / - 5
Copy the code
  1. Left move operator<<

What is the left shift operator? Move the digit number N to the left and add N zeros to the right. The left digit is discarded

Example: 2 << 2 = 8

The decimal system = The fourth The third The second The first
2 = 0 0 1 0
8 = 1 0 0 0

2 = 0010

8 = 1000

The rule is that number star is equal to 2 to the n

  1. Left move operator>>

This operator moves the first operand to the right by the specified number of digits. Bits moved to the right are discarded and the leftmost bits are copied to fill the left side. Since the new leftmost bit is always the same as before, the sign bit is not changed. That’s why it’s called “symbol propagation.”

Personal simple understanding, is the example: 8 >> 2 = 2

The decimal system = The fourth The third The second The first
8 = 1 0 0 0
2 = 0 0 1 0

8 = 1000

2 = 0010

See article – an in-depth study of Js bit operations and usage