An operator

Bitwise operators evaluate binary directly

Review: source code, inverse code, complement

Source code: a binary number with a sign bit as the first digit, with a positive sign of 0 and a negative sign of 1

Inverse code :(to solve the problem of adding pluses and minuses) the inverse of a positive number is equal to its original code, the inverse of a negative number retains the original sign bit, and the other bits are reversed

Complement :(to solve the inverse +0 and -0) the complement of a positive number is itself, and the complement of a negative number is added to the inverse by 1

In computer operations, complement is used to perform operations

And (&)

If the binary bits of both numbers are 1, it is 1; otherwise, it is 0

Example:

let a = 5; //00000000 00000000 00000000 00000101
a = a & 4; //00000000 00000000 00000000 00000100
console.log(a); //00000000 00000000 00000000 00000100
//4
Copy the code

Purpose: It can be used to judge the parity

// for(let I =0; i<4; I ++) {if(I & 1) {console.log(" odd ", I); }else {console.log(" even ", I); }}Copy the code

Or (|)

If there is at least one 1 in the binary bit corresponding to two numbers, it is 1; otherwise, it is 0

let a = 5; //00000000 00000000 00000000 00000101
a = a | 4; //00000000 00000000 00000000 00000100
console.log(a); //00000000 00000000 00000000 00000101
//5
Copy the code

An exclusive or (^)

If two numbers correspond to the same binary bit, it is 0; otherwise, it is 1

let a = 5; //00000000 00000000 00000000 00000101
let b = a ^ 4; //00000000 00000000 00000000 00000100
console.log(b); //00000000 00000000 00000000 00000001
//1
Copy the code

Purpose: Can be used to exchange values

let a = 5; let b = 2; a ^= b; b ^= a; a ^= b; console.log(a, b); / / 2, 5Copy the code

Not (~)

Operation current binary changes from 1 to 0 and from 0 to 1

let a = 7; //00000000 00000000 00000000 00000111 console.log(~a); 11111111 11111111 11111111 11111111 11111111 11111000 Original code //10000000 00000000 00000000 00000111 reverse code = sign bit unchanged, //10000000 00000000 00000000 00001000 + 1 = -8 let a = -7; //10000000 00000000 00000000 00000111 console.log(~a); / / 6 / / negative first calculate the complement after calculation, the changes in the ~ / / the source-code / / 11111111 10000000 00000000 00000000 00000111 11111111 11111111 11111000 radix-minus-one complement = the sign bit is changeless, For the rest, take inverse //11111111 11111111 1111111001 complement = inverse + 1, namely -8 // bit non-change //00000000 00000000 00000110, namely, the result is 6Copy the code

Conclusion: ~a = – (a+1);

The left < <

All bits of binary are moved to the left, the highest bits are discarded, and the lowest bits are filled with zeros

let a = 7; //00000000 00000000 00000000 00000111 let b = a << 1; // Move the binary to the left and fill console.log(b) with 0 on the right; 14 / / / / 00000000, 00000000, 00000000, 00001110 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- negative left the let a = 7. //10000000 00000000 00000000111 let b = a << 1; // Move the binary to the left and fill console.log(b) with 0 on the right; //10000000 00000000 00000000 00000111 A source code // 1111111111 11111111 11111111 11111000 inverse code // 1111111111 11111111 111111 11111001 complement, A complement // 1111111111 11111111 11111111 11110010 left shift, Is the original code of B //10000000 00000000 00000000 00001101 inverse code //10000000 00000000 00000000 00000000 00001110 complement code of B is -(8+4+2)=-14Copy the code

Summary: A <<n is a*2^n

Sign move right >>

All bits of binary move to the right, the low digit is discarded, and the value of the high digit is always the leftmost digit from the beginning, that is, the sign bit is always unchanged

let a = 7; //00000000 00000000 00000000 00000111 let b = a >> 1; //00000000 00000000 00000000 00000011 Discard the low value, copy the value to the left, and fill it up. console.log(b); //3 //------------------------- negative number left shift let a = -7; //10000000 00000000 00000000 00000111 let b = a >> 1; //a console.log(b); //a console.log(b); // b console.log(b); / / / / - 4 first to convert a complement the source-code / / / / 10000000 00000000 00000000 00000111 11111111 11111111 11111111 11111000 radix-minus-one complement / / 11111111 11111111 11111111 11111001 A's complement // Signed right //11111111 11111111 11111111 11111111 11111100 B's original code //10000000 00000000 00000000 00000011 Reverse code //10000000 00000000 00000000 00000100 complementCopy the code

Summary: A >>n is a/(2^n).

Unsigned move right >>>

All bits of binary are moved to the right, the lower bits are discarded, and the higher bits are filled with zeros. So it’s always positive

let a = 7; //00000000 00000000 00000000 00000111 let b = a >>> 1; //00000000 00000000 00000000 00000011 Discard the low value, copy the value to the left, and fill it up. console.log(b); //3 //------------------------- negative number left shift let a = -7; //10000000 00000000 00000000 00000111 let b = a >>> 1; //a console.log(b); //a console.log(b); // b console.log(b); / / 2147483644Copy the code

conclusion

Bit operators may be rarely used when writing things, but in the algorithm, look at the underlying code will encounter bit operations, learning to remember is still very necessary. Wait for the day to do the algorithm to determine the parity directly throw &, hey, hey, hey