Detailed js Bitwise Operators

Foreword

Usual numerical operations, in fact, is then first converted into binary arithmetic, and bit binary operation is a direct operation.
Bitwise arithmetic operation is lower, it is often the fastest speed (relative to other operations such as addition, subtraction, it), and by means of bit operation characteristic also enables a number of algorithms. Proper use of computing has many advantages.
Previous binary bit computing gives us the simple operation of a computer, but we had little contact with the bit operation.
All bits in the binary operations are performed down operation, then the binary 0 and 1 only.

Bitwise Operators table

Bit operation is divided into two, Bitwise and shift operators .
Here Insert Picture DescriptionHere Insert Picture Description
Bitwise - Logical Results Referring to Table
Here Insert Picture Description
Bitwise - Results Referring to Table
Here Insert Picture Description

Bitwise basis

&
Bitwise and
if the two corresponding bits are 1, then the 1-bit result value, otherwise 0
|
bitwise or the
two corresponding bits as long as there is a 1, this bit result value 1
^
bitwise XOR
the same values if the two bits to participate in the operation and 0, otherwise 1
~
negation
~ is a unary operator, is used for a binary bitwise, i.e. 0 to 1, 1
<<
left
for all bits of each of a number of N-bit left shift, a right complement 0
>>
right shift
each binary bit to the right a number of bits N, the low to the right end are discarded, for unsigned , high fill 0

Use bit arithmetic

(1) bitwise negation
operator rule is: after inversion binary operator, 1,1 becomes 0 becomes 0, so that a number of the result is negated even number itself.

0000 0000 0000 0000 0000 0000 0000 0011     -> 3
1111 1111 1111 1111 1111 1111 1111 1100     -> ~ 3 = -4

Common scenarios:
Negation: ~ a + 1

(2) << left shift
operator rules: each binary entire left several upper bits discarded, low 0s.
For example: 2 = 24 << 6

0000 0000 0000 0000 0000 0000 0000 0110     -> 6
0000 0000 0000 0000 0000 0000 0001 1000     -> 6 << 2 = 24

We binary two to 6 moves to the left, two on the lower fill 0, discards high, get out of the result is 24.

Common scenarios:
left is often used to make * (2 ^ n) operation, because the direct-based binary arithmetic, so the left efficiency ratio * (2 ^ n) high.

(3) >> right shift
operator rules: the right of each binary Several all, the high bit positive number 0, 1 negative high up, low discarded.
For example: 2 = 3 12 >>

0000 0000 0000 0000 0000 0000 0000 1100     -> 12
0000 0000 0000 0000 0000 0000 0000 0011     -> 12 >> 2 = 3

Because 12 is a positive number, right shift process on both the high bit 0, the low discarded, the result is obtained out of 3.
For example: -12 >> 2 = -3

1111 1111 1111 1111 1111 1111 1111 0100    -> -12
1111 1111 1111 1111 1111 1111 1111 1101    -> -12 >> 2 = -3

Because -12 is negative, the high bit right shift process on both 1, discarding low, get out of the result is -3.

Common scenarios:
right often used for / (2 ^ n) operation, because the direct-based binary arithmetic, so the right ratio efficiency / (2 ^ n) high.

(4) >>> unsigned right shift
operator rule: all right of each binary several upper bits of 0s, low discarded.
For example: 12 = 3 >>> 2

0000 0000 0000 0000 0000 0000 0000 1100     -> 12
0000 0000 0000 0000 0000 0000 0000 0011     -> 12 >>> 2 = 3

We'll binary two 12 moves to the right, high up on two 0, low discard, get out of the result is 3.
For example: -12 >>> 2 = 1073741821

1111 1111 1111 1111 1111 1111 1111 0100    -> -12
0011 1111 1111 1111 1111 1111 1111 1101    -> -12 >> 2 = 1073741821

We will be binary-12 to the right two, high up on two 0, low discard, get out of the result is 1073741821.

(5) & bits of
operator rule is: the operator on both sides of zero, the result is 0, 1 only when both sides are at the same time, the result was only 1.
As follows:
0 & 0 = 0; 1 & 0 = 0; 1 & 0 = 0; 1 & 1 = 1;
for example: 3 & 5

0000 0000 0000 0000 0000 0000 0000 0011     -> 3
0000 0000 0000 0000 0000 0000 0000 0101     -> 5
0000 0000 0000 0000 0000 0000 0000 0001     -> 3 & 5 = 1

Bits and special purpose computing:
a cleared (0 unit and a calculation result for bit zero)
two, taking a specified number of bits (e.g., taking num = 1010 1101 low four num & 0xF will give 00001101) .
Third, the parity is determined: place if (a% 2 == 0) with if ((a & 1) == 0) is determined is not a even number.

(6) | bits or
operational rules are on both sides of an operator, as a result, only when both sides simultaneously 0, the result was zero.
As follows:
0 | 0 = 0; 0 | =. 1. 1;. 1 | = 0. 1;. 1 | =. 1. 1;
for example: 3 | 5

0000 0000 0000 0000 0000 0000 0000 0011     -> 3
0000 0000 0000 0000 0000 0000 0000 0101     -> 5
0000 0000 0000 0000 0000 0000 0000 0111     -> 3 | 5 = 7

Also, negative press complement form to participate in a bitwise OR.

Be used:
The following is an excerpt from the HashMap class method, the algorithm used to modify the user passed in the constructor of the size, shift and use the algorithm is implemented or in combination, the performance is better than the cycle determination.

public static final int tableSizeFor(int cap) {
    int n = cap - 1;
    n |= n >>> 1;
    n |= n >>> 2;
    n |= n >>> 4;
    n |= n >>> 8;
    n |= n >>> 16;
    return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}

(7) ^ Bitwise XOR
operation rule is: when the same position on both sides of the operator are the same, the result returns 0, 1 are not the same return.
For example: 5 ^ 3 = 1

0000 0000 0000 0000 0000 0000 0000 0011     -> 3
0000 0000 0000 0000 0000 0000 0000 0101     -> 5
0000 0000 0000 0000 0000 0000 0000 0110     -> 3 ^ 5 = 6

Usually we exchange two numbers will be used to help a temporary variable:

int t = a;
a = b;
b = t;

However, using the bit operation, without the aid of the third variable.

a ^= b;
b ^= a;
a ^= b;

Reference:
B station video tutorial
thorough understanding Bitwise Operators
Bitwise Operators
Bitwise in js

Guess you like

Origin blog.csdn.net/weixin_44198965/article/details/94167806