The previous words
Bit operators are very underlying operations and are not commonly used because they are not intuitive. However, it is extremely fast and can achieve good results when used reasonably. This article will introduce the often overlooked operator in JavaScript - bit operator
Binary representation
All values in ECMAScript are stored in IEEE-754 64-bit format, but the bit operator does not directly operate the 64-bit value, but is calculated as a 32-bit signed integer, and the return value is also a 32-bit signed integer.
This bit conversion allows both values to be treated as 0 when applying bit operations to special NaN and Infinity values
If a bit operator is applied to a non-numeric value, the value will be converted into a numeric value using Number() first, and the result is a numeric value
//'|' means bitwise or, an integer and 0 bitwise or operation can obtain itself, and a decimal and 0 bitwise or operation can obtain the rounding effect console.log( 1.3 | 0);//1console.log( 1.8 | 0);//1console.log( Infinity | 0);//0console.log( -Infinity | 0);//0console.log( NaN | 0);//0console.log('12px' | 0);//0console.log('12' | 0);//12A signed integer uses the first 31 of the 32-bit to represent the integer value, the 32-bit to represent the integer symbol, 0 represents the positive number, and 1 represents the negative number. The bit representing a symbol is called a sign bit, and the value of the sign bit determines the format of other bit values. Among them, positive numbers are stored in pure binary format, and each of the 31 bits represents a power of 2. The first bit (called bit 0) represents 0 times of 2, the second bit represents 1 time of 2, and so on. Unused bits are filled with 0, i.e. they are ignored.
For example, the binary representation of the numerical value 18 is 000000000000000000000000000010010, or the more concise 10010. These are 5 valid bits, and these 5 bits themselves determine the actual value
console.log((18).toString(2));//"10010"
console.log(0b0000000000000000000000000000000010010);//18
Negative numbers are also stored in binary, but the format used is in two's complement. To calculate a numerical two's complement, you need to go through the following 3 steps:
【1】Find the binary code of the absolute value of this numeric value
【2】Find binary inverse code, that is, replace 0 with 1, replace 1 with 0
【3】The binary inverse code obtained is added to 1
For example, to determine a binary representation of -18, you must first get a binary representation of 18, as shown below:
0000 0000 0000 0000 0000 0000 0001 0010
Next, calculate the binary inverse code as follows:
1111 1111 1111 1111 1111 1111 1111 1110 1101
Finally, add 1 to the binary inverse code, as follows:
1111 1111 1111 1111 1111 1111 1110 1101 1-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Therefore, the binary representation of -18 is 1111 1111 1111 1111 1111 1111 1111 1111 1110 1110
ECMAScript will try its best to hide all this information from us. When outputting a negative number in the form of a binary string, all we see is that the binary code with the absolute value of this negative number is preceded by a negative sign.
var num = -18;console.log(num.toString(2));//'-10010'
The bit operator can perform 7 types of operations, including bitwise non-(NOT), bitwise and (AND), bitwise or (OR), bitwise exclusive or (XOR), left shift, signed right shift and unsigned right shift.
Bitwise non-(NOT)
The bitwise non-operator is represented by a wavy line (~). The result of executing the bitwise non-operator is to return the inverse code of the value. Its essence is to minus the negative value of the operand by 1
var num1 = 25;var num2 = ~num1;console.log(num2);//-26
You can get the rounding effect by double bitwise for an integer; you can get the rounding effect by double bitwise for a decimal.
console.log(~~3);//3console.log(~~3.1);//3console.log(~~3.9);//3
Bitwise and (AND)
The bitwise and operator is represented by a sum symbol (&), which has two operator numbers. Essentially, bitwise and operation is to align each bit of two values, and then perform an AND operation on two numbers at the same position according to the rules in the following table.
The first value bit of the second value bit result 1 1 11 0 00 1 00 0 0 0 0
Bitwise and operation will return 1 only if the corresponding bits of the two values are 1. Any bit is 0, and the result is 0.
var iResult = 25 & 3;console.log(iResult);//"1"
//Analysis is as follows 25 = 0000 0000 0000 0000 0000 0000 0001 1001 3 = 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001
bitwise or (OR)
The bitwise or operator is represented by a vertical symbol (|), and there are also two operands. The bitwise or operation follows the following truth table
The first value bit of the second value bit result 1 1 11 0 10 1 10 0 0 0
Bitwise or operation returns 1 if one bit is 1, and only if both bits are 0.
var iResult = 25 | 3;console.log(iResult);//"27"
//Analysis is as follows 25 = 0000 0000 0000 0000 0000 0000 0001 1001 3 = 0000 0000 0000 0000 0000 0000 0000 0000 0001 1011
An integer and 0 bitwise or operation can get it, and a decimal and 0 bitwise or operation can get the rounding effect
console.log(3.1 | 0);//3console.log(3.9 | 0);//3
Bitwise XOR (XOR)
The bitwise XOR operator is represented by a caret (^) and has two operands. The following is the truth table of bitwise XOR
The bit of the first value The bit of the second value 1 1 01 0 10 1 10 0 0 0
Returns 0 when the two values of bitwise XOR are the same, and return 1 when not at the same time
var iResult = 25 ^ 3;console.log(iResult);//"26"
//Analysis is as follows 25 = 0000 0000 0000 0000 0000 0000 0001 1001 3 = 0000 0000 0000 0000 0000 0000 0000 0000 0001 1010
"Exoroor operation" has a special use, which performs three XOR operations on two numbers a and b in succession, aˆ=b, bˆ=a, aˆ=b, and their values can be swapped. This means that using "exclusive OR operation" can interchange the values of two variables without introducing temporary variables
var a=10,b=9;a ^= b, b ^= a, a ^= b;console.log(a,b);//9,10
//Analysis is as follows a = 0000 0000 0000 0000 0000 0000 0000 0000 1010 b = 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0011 b = 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0 1001--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
An integer with 0 bitwise XOR can keep itself, and a decimal with 0 bitwise XOR can be rounded
console.log(3.1 ^ 0);//3console.log(3.9 ^ 0);//3
Move left
The left shift operator is represented by two less than signs (<<). This operator will move all bits of the value to the left by the specified number of bits.
For example, if the value 2 (binary code is 10) is shifted 5 bits to the left, the result is 64 (1000000)
var oldValue = 2;var newValue = oldValue<<5;console.log(newValue);//64
Moving left will not affect the sign bit of the operand. In other words, if -2 is moved 5 bits to the left, the result will be -64
var oldValue = -2;var newValue = oldValue<<5;console.log(newValue);//-64
The left shift 0 bits can achieve rounding effect
console.log(3.1 << 0);//3console.log(3.9 << 0);//3
Signed right
The signed right-shift operator is represented by two greater than signs (>>), which moves the value to the right, but retains the sign bits (i.e., sign marks). The signed right shift operation is exactly the opposite of the left shift operation, that is, if 64 is moved 5 bits to the right, the result will be changed back to 2.
var oldValue = 64;var newValue = oldValue>>5;console.log(newValue);//2
Similarly, during the shifting process, vacancy will also appear in the original value. However, this time the vacancy appears on the left side of the original value and on the right side of the sign bit. At this time, ECMAScript will fill all empty spaces with the value of the sign bit to get a complete value
Move right to simulate the divisor operation of 2
console.log(5>>1);//2console.log(15>>1);//7
Unsigned right
The unsigned right shift operator is represented by 3 larger than signs (>>>), which moves all 32 bits of the value to the right. For positive numbers, the result of unsigned right shift is the same as the signed right shift. It is convenient to move the sign right in front of it. If you move the unsigned 64 by 5 bits, the result is still 2
var oldValue = 64;var newValue = oldValue>>>5;console.log(newValue);//2
However, the negative number is different. First, the unsigned right shift fills the empty bit with 0, instead of filling the empty bit with the value of the signed bit like the signed right shift. Therefore, the result of unsigned right shift to a positive number is the same as the result of a title shift to a title, but the result of a negative number is different. Secondly, the unsigned right shift operator will treat the negative binary code as the positive binary code. Moreover, since the negative number is represented in the two's complement of its absolute value, the result will be very large after the unsigned right shift.
var oldValue = -64;var newValue = oldValue>>>5;console.log(newValue)//134217726
To determine the binary representation of -64, you must first get the binary representation of 64, as shown below:
0000 0000 0000 0000 0000 0000 0100 0000
Next, calculate the binary inverse code as follows:
1111 1111 1111 1111 1111 1111 1111 1011 11111
Finally, add 1 to the binary inverse code as shown below
1111 1111 1111 1111 1111 1111 1111 1011 1111 1----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
After moving 5 bits to the right, as shown below:
0000 0111 1111 1111 1111 1111 11111 11111 1110console.log(0b000001111111111111111111111111111111111111111111111111111110);//134217726
Common Applications
【1】Multiple operation
Use left shift (<<) to realize multiplication operation
console.log(2 << 1);//4console.log(3 << 1);//6console.log(4 << 1);//8
【2】Divide operation
Use signed right shift (>>) to simulate the divisor operation of 2
console.log(2 >> 1);//1console.log(5 >> 1);//2console.log(8 >> 1);//4console.log(9 >> 1);//4console.log(9 >> 1);//4
【3】Value swap
The effect of value interchange can be achieved using XOR operation (^)
var a=10,b=9;a ^= b, b ^= a, a ^= b;console.log(a,b);//9,10
【4】Summarize the decimal
The decimal rounding effect can be achieved by taking two bitwise non-bits, 0 bitwise OR, 0 bitwise OR, 0 bits left and 0 bits right.
console.log(~~3.1);//3console.log(3.1|0);//3console.log(3.1^0);//3console.log(3.1<<0);//3console.log(3.1>>0);//3console.log(3.1>>0);//3
【5】Switch
The bit operator can be used as a switch to set object properties. Assume that an object has four switches, each switch is a variable. Then, you can set a four-bit binary number, each of which corresponds to a switch.
var FLAG_A = 1; // 0001var FLAG_B = 2; // 0010var FLAG_C = 4; // 0100var FLAG_D = 8; // 1000
The above code sets four switches A, B, C, and D, each switch has a binary bit respectively.
Now suppose that the three ABD switches need to be turned on, we can construct a mask variable
var mask = FLAG_A | FLAG_B | FLAG_D;// 0001 | 0010 | 1000 => 1011
The above code performs "or operation" on three variables ABD to obtain 1011 with a mask value of binary
// "Operation" ensures that the specified switch is turned on flags = flags | mask;
// "Association" can turn off all items in the current setting that are different from the switch settings. flags = flags & mask;
// "Exclusive OR operation" can switch to (toggle) the current setting, that is, the reverse value of the current setting can be obtained for the first time, and the original value can be obtained by executing again. Flags = flags ^ mask;
// "No operation" can flip the current setting, that is, the original setting is 0, and it becomes 1 after the operation; the original setting is 1, and it becomes 0flags after the operation = ~flags;
The above comprehensive introduction to the javascript operator - bit operator is the entire content shared by the editor. I hope it can give you a reference and I hope you will support Wulin.com more.