Relevant concepts

Concept: Bit operation operating at the base of a number (representing the 32 digits of a number). Because it is the low-level operation of the operation, so the operation speed is often the fastest, but he is not intuitive.

Bitwise operations only work on integers. If an operation is not an integer, it is automatically converted to an integer before being run.

Inside JavaScript, values are stored as 64-bit floating-point numbers, but bitwise operations are performed as 32-bit signed integers and return values as 32-bit signed integers.

This conversion makes it possible to treat special NaN and Infinity values as zero when bitwise operations are applied to them. For non-numeric bitwise operations, the **Number()** method is used to convert the value to a numeric value before applying the bitwise operation.

Binary: ECMAScript integers have two types, namely signed integers (positive and negative numbers are allowed) and unsigned integers (only positive numbers are allowed). In ECMAScript, all integer literals are signed integers by default.

The signed integer uses the 31st bit to represent the value of the integer, the 32nd bit to represent the symbol of the integer, 0 for a positive number and 1 for a negative number. The value ranges from -2147483648 to 2147483647. The symbol bit is called the symbol bit, and the value of the symbol bit determines the format of the other bit values. Where positive numbers are stored in pure binary format, each of the 31 bits represents a power of 2, the first (called bit 0) represents a power of 2 and so on, and the unused bits are padded with zeros and can be ignored.

Example analysis

For example, the representation of the number 10

The binary of 10 is’ 1010 ‘, and it uses only the first four bits, which are significant bits. The rest of the digits are negligible.

console.log((10).toString(2)); / / 1010Copy the code

The back

Negative numbers can store bits of binary code, but in the form of a binary complement, which is computed as follows:

1. Determine the binary representation of the non-negative version of a number

2. To find the binary inverse, replace the 0 with 1, and the 1 with 0

3. Add 1 after binary reverse code

For example, determine the binary complement of -10

Step 1: the binary representation of 10 is 0000 0000 0000 0000 1010

Step 2: Swap 1 with 0 1111 1111 1111 1111 1111 1111 0101

Step 3: binary inverse +1

So the binary representation of -10 is 1111 1111 1111 1111 1111 1111 0110. One thing to note when dealing with signed integers is that 31 bits are not accessible. However, after converting negative integers to binary strings, ECMAScript does not display them as a binary complement. Instead, ECMAScript prints them as the standard binary with the absolute value of the number preceded by a negative sign to avoid accessing bits 31 for convenience.

For example, console.log((-10).tostring (2)) // -1010Copy the code

Bitwise operations can perform seven operations, including bitwise NOT (NOT), bitwise AND (AND), bitwise OR (OR), bitwise XOR, left shift, signed right shift, AND unsigned right shift

A concrete analysis

1, bitwise NOT (NOT)

The bitwise nonoperator, represented by a wavy line (~), returns the inverse of the numeric value, essentially subtracting the negative value of the operand by 1

<script> let n = 9; Let I = 9.9; let m = ~n; console.log(m); //-10 console.log(~~n); // console.log(~~ I); // console.log(~~ I); </script> </script>Copy the code

2, AND (AND)

The bitwise and operator is represented by an and (&), which consists of two operands. Operate directly on the binary form of a number. It aligns the digits in each digit, AND then applies the following rules to two digits in the same position. Bitwise and operands return 1 only if the corresponding bits of both values are 1, and the result is 0 if either bit is 0

<script>        
    let n = 10;        
    let m = 15;        
    console.log(n & m);  //10        
    console.log(n.toString(2), m.toString(2)); //1010 1111  
</script>
Copy the code

Analysis is as follows:

3, by position OR (OR)

Bitwise or operators are represented by a vertical line (|) symbol, and there are also two operators. Bitwise or operations follow the following table. Returns 1 if one digit is 1, and 0 only if both digits are 0. An integer can be placed in place with 0 to get itself, and a decimal can be placed in place with 0 to get the effect of rounding.

<script> let n = 10; let m = 15; console.log(n | m); //15 console.log(n.toString(2), m.toString(2)); / / 1010 1111 console. The log (3.9 | 0); / / 3 console. The log (4.1 | 0); //4 </script>Copy the code

4. Bitwise XOR

It is represented by a caret (^) and also requires two operands. Here is the truth table. Returns 0 if two values of bitwise xor are the same, and 1 if not. An integer or 0 can hold itself bitwise, and a decimal or 0 can be rounded

<script>
        let n = 10;
        let m = 15;
        console.log(n ^ m);  //5
        console.log(n.toString(2), m.toString(2)); //1010 1111 
</script>
Copy the code

^ There is a special use of three consecutive xor operations on two numbers a and b, a^=b,b^=a,a^=b can be swapped. This means that using ^ allows you to swap the values of two variables without introducing temporary variables.

<script>
        let a = 10, b = 11; // 1010   |   1100
        a ^= b;  // 1010  ^  1100  = 0110
	b ^= a; //  1100 ^ 0110 = 1010
	a ^= b; //  0110 ^  1010 = 1100
        console.log(a, b); //11 10
</script>
Copy the code

5, shift to the left

The left-shift operator, represented by two less-than signs (<<), moves all bits of a numeric value to the left by the specified number of digits.

For example, if you move the number 3 (binary 11) five places to the left, you get 96

<script>
       let n=3;
       let m=n<<5;
       console.log(n.toString(2)); //11
       console.log(m); //96 
</script>
Copy the code

6. There is a sign shift to the right

Represented by two greater than signs (>>), the value is moved to the right, reserving the sign bit. A signed right-shift operation is the opposite of a left-shift operation, such as 96 moving five bits to the right to become 3.

7. Unsigned right shift

It is represented by three greater than signs (>>>). This operator moves all 32 bits of the value to the right. For positive numbers, an unsigned right shift is the same as a signed right shift. But for negative numbers, the unsigned right shift fills the space with 0. Another point is that the unsigned right-shift operator treats a negative binary as a positive binary and since negative numbers are represented as the binary complement of their absolute values, the unsigned right-shift result is very large.

For example, -95 moves 5 to the right unsigned

<script> let n = -96; console.log(n >>> 5); //134217725 </script>Copy the code

Analysis:

Three, common operations

Use << to achieve multiplication; Use >> to achieve division operation; Swap values using ^; Small trees take the whole;

<script> console.log(4 << 1); //8 4* math.pow (2,1) console.log(4 << 2); // 4* math.pow (2,2) console.log(4 << 3); //32 4* math.pow (2,3) console.log(4 << 4); //64 4* math.pow (2,4) console.log(4 << 5); //128 4* math.pow (2,5) console.log(4 << 6); //256 4* math.pow (2,6) console.log(5 >> 3); //0 parseInt(5/ math.pow (2,3)) console.log(14 >> 2); //3 parseInt(5/ math.pow (2,2)) console.log(225 >> 5); //3 parseInt(5/ math.pow (2,2)) console.log(225 >> 5); //7 parseInt(5/ math.pow (2,5)) let a = 20, b = 4; a ^= b, b ^= a, a ^= b; console.log(a, b); / / 4 20 console. The log (~ 9.9); / / 9 console. The log (9.8 | 0); / / 9 console. The log (9.7 ^ 0); / / 9 console. The log (9.6 < < 0); / / 9 console. The log (9.3 > > 0); //9 </script>Copy the code