Most languages are provided by the operator according to the position, and the operating symbols are widely used in languages such as C, C ++, and there are not many application examples in scripts such as JS and AS. The operator will achieve good results.
Let's talk about the use of bit operations in JS (also applicable to other languages) according to your own cognition. If you have any errors, please correct me.
The operating number of the position is to consider the number of operations as a series of separate positions, not a digital value. So before that, I have to mention what "bit" is:
Numerous or characters are stored in the memory as 0 and 1 sequences. Each 0 and 1 is called 1 digit. 0. When we change the bit value inside the memory, the significance of this value changes. For example, move one digit before 2, and now the storage unit becomes 0 0 0 0 0 0 1 0 0. It is a decimal 4, which is the principle of the operation of the operating symbol.
There are 6 according to the operational operator
& Press the position and
| Press or
^Depending on position or
~ Take the opposite
>> Right shift
<< shift
1 & operator
& Is a dual operator. It combines the corresponding bit in a specific way in a specific way. If the corresponding bit is 1, then the result is 1, if any bit is 0, the result is 0
The result of 1 & 3 is 1
Let's see how it runs:
1 binary representation is 0 0 0 0 0 0 0 1
The binary of 3 indicates 0 0 0 0 0 0 0 1 1
The result obtained according to the rules of & is 0 0 0 0 0 0 0 0 1, decimal representation is 1
As long as the result of the 0 & operation is 0, so you can use the unnecessary bit of a variable to be set to 0. 4 bits, eliminate the high 4 -bit & 0x0f (Living: 0x0F is hexadecimal representation, the corresponding binary is 0 0 0 0 1 1 1). This feature has a very important application. It will be later. Mention.
2 | operator
The difference between | and & is that if any operation in the corresponding bit is 1, then the result is 1
1 | 3 results are 3
3 ^ operator
^The operational symbol is similar to |, but one thing is that if both operating positions are 1, the result is 0
0 1 0 0 0 0 0 1
0 1 0 1 1 0 1 0
Generate 0 0 0 1 1 0 1 1 1
4 ~ operator
!
5 The displacement operator of the displacement operator to move the position to the left or right according to the specified value
<< Move to the left >> move to the right, the more than the position will be lost, and the empty position will make up 0
For example
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 (Ten Metal 12)
Move to the right is two bits
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (Twita 4096)
Here are some specific applications
As mentioned earlier, 2 move forward 1 bit to 4 to use this feature can be used for multiplication operations
2 << 1 = 4
3 << 1 = 6
4 << 1 = 8
Similarly >> Then you can do dividends
Any decimal >> 0 can be taken up
Such as 3.14159 >> 0 = 3;
^The operation service has a magical feature
If the following code
Copy code code as follows:
<script>
var n1 = 3;
var n2 = 4;
n1 ^= n2;
n2 ^= n1;
n1 ^= n2;
</script>