Most languages provide bitwise operators, which are widely used in languages such as c,c++, while there are not many application examples in scripting languages such as Js,as, and the appropriate use of bitwise operators can be a good result.
Below according to own cognition simple talk about the bit operation use in JS (same applies to other languages), if have mistake, welcome correct correction.
Bitwise operators treat operands as a series of individual bits, not as a numeric value. So before that, you have to mention what a "bit" is:
Values or characters are stored in memory as a sequence of 0 and 1, each 0 and 1 are called 1 bits, for example, 10 data 2 is stored in the computer as 0 0 0 0 0 0 1 0, and when we change the bit value in memory, the value represents a change, such as moving 2 forward, Now the storage unit has become 0 0 0 0 0 1 0 0, this value represents the decimal 4, which is the operation principle of bitwise operator.
Bitwise operators have 6
& Bitwise AND
| bitwise OR
^ per-bitwise XOR OR
~ Take the Counter
>> Move Right
<< Move Left
1 & operator
& is a two-dollar operator that combines the corresponding bits of the operand in a particular way if the corresponding bit is 1, the result is 1, and if any one bit is 0, the result is 0.
1 & 3 Results for 1
To see how it works:
The binary representation of 1 is 0 0 0 0 0 0 1
The binary representation of 3 is 0 0 0 0 0 1 1
According to the & rules, the result is 0 0 0 0 0 0 0 1, the decimal representation is 1
As long as any one of the 0 & operations results is 0, so you can use & to set a variable of the unnecessary bits to 0, such as the binary of a variable for 0 1 0 0 1 0 0 1, I would like to keep a low 4, eliminate high 4 bits with & 0x0f on it (live: 0x0f is 16) notation, the corresponding binary is 0 0 0 0 1 1 1 1), and this feature has a very important application that will be mentioned later.
2 | Operator
| The difference with & is that if either operand in the corresponding bit is 1 then the result is 1.
1 | 3 of the results are 3
3 ^ operator
^ Operators with | Similar, but there is a difference is that if two operating bits are 1, the result produces 0
0 1 0 0 0 0 0 1
0 1 0 1 1 0 1 0
Produces 0 0 0 1 1 0 1 1
4 ~ operator
~ is to the position of the reverse 1 change 0, 0 to 1
5 shift operator shift operator moves a bit to the left or right by the specified value
<< move left and >> move to the right, more than the bit will be lost, and the vacated bit is 0
For example 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 (decimal 16387) move to the left two digits will become
0 0 0 0 0 0 0 0 0 0 0 1 1 0 (decimal 0)
Move to the right two digits is
0 0 0 1 0 0 0 0 0 0 0 0 0 0 (decimal 0)
Here are some specific applications
mentioned above 2 move forward 1 bits into 4 Use this feature to do multiplication operations
2 << 1 =4
3 << 1 = 6
4 << 1 = 8
Empathy >> can do division operations
Any decimal place it >> 0 can be rounded
such as 3.14159 >> 0 = 3;
^ The operator has a magical feature
such as the following code
Copy Code code as follows:
<script>
var n1 = 3;
var n2 = 4;
N1 ^= N2;
N2 ^= N1;
N1 ^= N2;
</script>