Binary bit operators are used to directly calculate binary bits, there are 7 in total.
-Binary OR operator (or): The symbol is
|, which means that if both binary bits are
0, the result is
0, otherwise it is
1. -Binary AND Operator (and): The symbol is
&, which means that if both binary bits are 1, the result is 1, otherwise it is 0. -Binary negation operator (not): The symbol is
~, which means to invert a binary bit. -Exclusive OR operator (xor): The symbol is
^, which means that if the two binary bits are not the same, the result is 1, otherwise it is 0. -Left shift operator (left shift): The symbol is
<<, see the explanation below for details. -Right shift operator (right shift): The symbol is
>>, see the explanation below for details. -** Zero filled right shift operator ** (zero filled right shift): The symbol is
>>>, see the explanation below for details.
These bit operators directly deal with each bit (bit), so they are very low-level operations. The advantage is that they are extremely fast, but the disadvantage is that they are very unintuitive. They cannot be used in many situations, otherwise the code will be difficult to understand and error-checking.
i = i | 0;
The meaning of the above line of code is to convert
i (whether it is an integer or a decimal) to a 32-bit integer.
Using this feature, you can write a function to convert any value to a 32-bit integer.
Binary AND Operator
The rule of binary AND operator (
&) is to compare two operators bit by bit, and only one of the two binary bits If the bit is
0, otherwise return
All bit operations are only valid for integers. When the binary negation operation encounters a decimal, the decimal part will be discarded, and only the integer part will be retained. Therefore, two consecutive binary negation operations on a decimal number can achieve the effect of rounding.
~~2.9; // 2 ~~47.11; // 47 ~~1.9999; // 1 ~~3; // 3
Rounding using binary negation is the fastest of all rounding methods.
Number function to convert the string to a numeric value.
XOR operator The
XOR operation (
1 when the two binary digits are different, and returns
0 when they are the same.
0 ^ 3; // 3
In the above expression,
11) are XORed, and their binary bits are different, so
3) is obtained.
There is a special application of "exclusive OR", which is to perform exclusive OR operation on two numbers
b three times in succession,
a^=b; b^=a; a^=b;, can be interchangeable their values. This means that the "exclusive OR operation" can exchange the values of two variables without introducing temporary variables.
var a = 10; var b = 99; (a ^= b), (b ^= a), (a ^= b); a; // 99 b; // 10
This is to swap the values of two variables The fastest way.
The exclusive OR operation can also be used for rounding.
12.9 ^ 0; // 12
Left shift operator The
left shift operator (
<<) means to move the binary value of a number to the left by a specified number of bits, with
0 at the end, That is, multiplied by the specified power of
2. When moving to the left, the sign bit of the highest bit is moved together.
Right shift operator with zero padding at the head, zero padding at the
head The only difference between the right shift operator (
>>>) and the right shift operator (
>>) is that when the binary form of a number moves to the right, the head will always be filled with zeros, regardless of the sign bit. Therefore, this operation always results in a positive value. For positive numbers, the result of this operation is exactly the same as the right shift operator (
>>), the difference lies mainly in negative numbers.
(4 >>> (1 - // 2 4)) >>> 1; // 2147483646 /* // Because the binary form of -4 is 11111111111111111111111111111100, // the signed bit is shifted one bit to the right to get 01111111111111111111111111111110, // It is 2147483646 in decimal. */
This operation actually converts a value into a 32-bit unsigned integer.
The fastest way to view the storage form of a negative integer in the computer is to use this operator.
-1 >>> 0; // 4294967295
The code above indicates that when
-1 is used as a 32-bit integer, the internal storage format is interpreted using unsigned integer format, and the value is 4294967295 (ie
(2^32)-1, which is equal to
Bit operators can be used as switches for setting object properties.
Suppose an object has four switches, and each switch is a variable. Then, you can set a four-digit binary number, each of which corresponds to a switch.
- Mozilla Developer Network, Bitwise Operators