In the last chapter we studied bitwise operators to introduce you to bitwise operators
In this chapter we look at the classic cases of bitwise operations
review
If the number is larger than 2^{31}, only the lower 32 bits are retained. If the number is higher than 32 bits, the lower 32 bits are discarded.
By bit and (&)
If two binary digits are 1, the result is 1; otherwise, the result is 0.
Convert a floating point number to an integer
//12 math.pi & math.pi //3Copy the code
By default, JavaScript stores numbers as 64-bit floating point numbers, but bitwise operations are performed as 32-bit binary integers.
When two same numbers are operated bitwise, only the integer part of the result is retained, and the fractional part is not stored during the conversion phase.
Infinity & Infinity //0
Number.MAX_VALUE & Number.MAX_VALUE //0
Copy the code
If a number is greater than 2312^{31}231, the result is inaccurate because only the lower 32 bits are retained during bit-counting, and the higher 32 bits are discarded.
Determine the parity of numbers
Function assert(x) {return x & 1; } assert(3) //1 If 1 is odd, otherwise even.Copy the code
If x is any positive integer and we use x = 3 as an example, the sequence of 3&1 in 32-bit bits is:
3 (base 10) = 00000000000000000000000000000011 (base 2)
1 (base 10) = 00000000000000000000000000000001 (base 2)
--------------------------------
3 & 1 (base 10) = 00000000000000000000000000000001 (base 2) = 1 (base 10)
Copy the code
3 & 1 is 1, which is odd. Here’s another example:
4 (base 10) = 00000000000000000000000000000100 (base 2)
1 (base 10) = 00000000000000000000000000000001 (base 2)
--------------------------------
4 & 1 (base 10) = 00000000000000000000000000000000 (base 2) = 0 (base 10)
Copy the code
4 & 1 is 0, which is even.
Count the number of 1s in binary
Write a function that takes an unsigned integer (as a binary string) as input and returns the number of ‘1’ digits in its binary expression (also known as the Hamming weight). .
function hammingWeight(x) {
count = 0
while(x){
x = x & (x - 1);
count++;
}
return count;
}
hammingWeight(6) //2
Copy the code
So let’s put in x is equal to 6, so x minus 1 is equal to 5, so what’s 5 and 6? Let’s draw it.
6 (base 10) = 00000000000000000000000000000110 (base 2)
5 (base 10) = 00000000000000000000000000000101 (base 2)
--------------------------------
6 & 5 (base 10) = 00000000000000000000000000000100 (base 2) = 4 (base 10) count++;
3(base 10) = 00000000000000000000000000000011 (base 2)
--------------------------------
4 & 3(base 10) = 00000000000000000000000000000000 (base 2) = 0 (base 10) count++;
Copy the code
We executed x = x & (x-1) twice, so count++ will be executed twice, and the third time we start while, x = 0, we break out of the loop and return 2;
Just to illustrate, because binary is full of 2 to 1, so x & (x – 1) just turns the lowest 1 in the binary bit of x into a 0. The number of times that x & (x – 1) xor is executed is the number of “1s”.
conclusion
- A binary digit that ends with 0 indicates an even number, and a binary digit that ends with 1 indicates an odd number
x & n - 1
It can always change the lowest bit of x from 1 to 0, leaving the other bits unchanged.