A brief analysis of the application of JavaScript bitwise operators

  • 2020-03-30 01:35:24
  • OfStack

Most languages provide bitwise operators, bitwise operators are widely used in c,c++ and other languages, while JS, AS and other scripting languages are not too many examples of application, sometimes, the proper use of bitwise operators will achieve good results.
Below according to their own cognition to talk about the use of bit operations in js (also applicable to other languages), if there is an error, welcome to correct.

Bitwise operators treat operands as a series of individual bits rather than as a numeric value. So before that, we have to mention what a bit is:
Numeric or character is to be stored in the memory for the sequence of 0 s and 1 s, each 0 and 1 are called 1 bits, such as decimal data is stored inside a computer 2 0 0 0 0 0 0 0 1, when we will change in the value of the memory, the significance of this value represents the will changed, such as the mobile before 2 a, now inside the storage unit into 1 0 0 0 0 0 0 0, this value represents the decimal 4, which is in accordance with the principle of operation of the operator.

There are six bitwise operators
& click with
| or by location
^ by bit or
To take the
> > Moves to the right
< < Shift to the left


1 & operator
Ampersand is a binary operator that combines the bits of an operand in a particular way so that if the bits are all 1, the result is 1, and if any of the bits are 0, the result is 0
1 minus 3 is 1
Here's how it works:
The binary representation of 1 is 0, 0, 0, 0, 0, 1
The binary representation of 3 is 0, 0, 0, 0, 1, 1
The result of the & rule is 0, 0, 0, 0, 0, 0, 1

As long as no one is 0 & operation result is 0, so you can use & set a variable unnecessary bit is 0, such as a variable of the binary representation of 0 0 0 1 0 0 1, I want to keep low four, eliminate high four it is ok to use & 0 x0f (live: 0 x0f to hexadecimal notation, the corresponding binary 0 0 0 0 1 1 1 1), this feature is a very important application, will be mentioned in the rear.

2 | operator
The difference between | and & is that if any of the corresponding bits have an operand of 1, the result is 1
1 |, 3 is 3

3 ^ operator
The ^ operator is similar to |, except that if both operands are 1, the result is 0
0, 1, 0, 0, 0, 0, 1
0, 1, 0, 1, 1, 0, 1, 0
It produces 0, 0, 0, 1, 1, 0, 1, 1

4 ~ operator
~ is the inverse of the counterpoint 1 to 0, 0 to 1

Shift operator the shift operator moves bits left or right at the specified value
< < Moving to the left. > If you move to the right, the bits passed will be lost, and the bits left empty will be filled with 0

For example, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1(decimal 16387) moves two digits to the left to become
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 (decimal 12)
If I move two to the right, I'm going to
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.


Here are some specific applications
I mentioned that 2 moves forward 1 bit to become 4 and you can multiply with this property
2 < < 1 = 4
3 < < 1 = 6
4 < < 1 = 8
In the same way > > You can do division


Any decimal put it > > You can round 0
Such as 3.14159 > > 0 = 3;

The ^ server has an amazing feature
The following code


<script>
var n1 = 3;
var n2 = 4;
n1 ^= n2;
n2 ^= n1;
n1 ^= n2;
</script> 


Related articles: