Bitwise operators in JS: "and, or, not, xor, shift left, shift right"

Javascript has arithmetic operators, assignment operators, comparison operators, logical operators, and bitwise operators.

Introduction

Think about it first, as follows, what answer should be output?


    console.log('5 & 1 :', ( 5 & 1 ));
    console.log('5 | 1 :', ( 5 | 1));
    console.log('~ 5:', (~5));
    console.log('5 ^ 1 :', ( 5 ^ 1));
    console.log('5 << 1 :', ( 5 << 1));
    console.log('5 >> 1 :', ( 5 >> 1));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Decimal to Binary

Convert the value to binary first. 
write picture description here

5 is divided by 2 and the remainder is 1, 2 is divided by 2 and the remainder is 0, and 1 is divided by 2 and the remainder is 1. Reverse the remainder and you get 5 in binary, 101. In the same way, the binary number 1 of 1 can be obtained.

Bitwise AND

AND operation rule: if two bits are "1" at the same time, the result is "1", otherwise it is 0 
5 & 1 = 1

bitwise or |

OR algorithm: The result of OR is 0 only if both bits are 0.

5| 1 = 5


Bitwise NOT~

Not Algorithm: Unary Operator

Binary original code: 0000 0000 0000 0000 0000 0000 0000 0101 
After inversion operation: 1111 1111 1111 1111 1111 1111 1111 1010 
Signed integers are represented by complement code, and complement code = complement + 1 
1. First complement code : 1000 0000 0000 0000 0000 0000 0000 0101 
2. Complement code: 1000 0000 0000 0000 0000 0000 0000 0110 The 
highest bit represents the sign bit 1 represents a negative number, 0 represents a positive number 
~5 = -6

Bitwise XOR^

XOR algorithm: if the two bits are different, the result is "1", otherwise it is 0 

5^1 = 4


/*
>> means a signed right shift 
>>> means an unsigned right shift

Signed right shift (>>): Shift the number to the right by a specified number of bits according to binary, the high bit such as the sign bit is positive and zero, and the sign bit is negative by one. , the low bit is directly removed
by unsigned right shift (>>>): Shift the number to the right by the specified number of bits according to binary, the high bit is directly filled with zeros, and the low bit is removed!
*/

Shift left bitwise

Left shift algorithm: shift the value to the left by a few bits, and add 0 to 
5<< 1 = 10

bitwise shift right

Shift Right Algorithm: Shift a value a few places to the right 

5>>1 = 2

Comma expression:
General form: expression1, expression2, expression3, ...expressionn

Solving process: first calculate the value of expression 1, then calculate the value of expression 2, ... until the value of expression n. The final value of the entire expression is the value of expression n. 

Take a look at the following examples:

 1) x = 8*2, x*4; /*The entire expression is a comma expression, its value is 64, and the value of x is 16*/

 2) (x = 8*2, x*4) , x*2; /*The whole expression is a comma expression, its value is 32, and the value of x is 16*/

 3) x = (z=5,5*2); /*The whole expression is an assignment expression, its value is 10, and the value of z is 5*/


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325902508&siteId=291194637