Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Welcome to the Nuggets, this is little Chetto _ ridiculous 😁😁.
Recently, I did a project with the backstage of our company and found an interesting thing. I really didn’t think of it before. Maybe we are too stupid. But it’s not too late to use it now, maybe it’s old technology, I don’t know, those who know can tell me, is it the way people used to use it?
What kind of technology is this?! It is the use of base bit operation to do user authority management. Very useful, and I’m going to divide it into three topics.
- JavaScript base bit operations
- Judgment of authority
- After use feeling
One – base bit operation
We need to know a little bit about base operations, both binary and hexadecimal, but fortunately they are the same.
I’ve been racking my brains all day trying to use this bit, and finally found that the way to use it is very simple, because JS has already integrated the usage for you, which makes it much easier to use. I am the kind of person who must understand the principle when using a technology. Otherwise, it will be very difficult to locate problems in the later stage if I am not clear about using a technology. So I hope you are the same. Bit arithmetic. I imagined a lot of data. I read a lot of blogs.
Without further ado, let’s get straight to the point…
1. JavaScript bit operations
1.1. The Number
Before we talk about bitwise operations, let’s take a quick look at JavaScript Number, which we’ll use later.
In JavaScript, numbers are double-precision 64-bit floating-point numbers based on IEEE 754 standards. The structure looks like this:
Sign bit: Used to indicate a plus or minus sign
Exponent: Used to express exponents
Mantissa (mantissa) : Used to indicate accuracy
That is, a number can only be between -(2^ 53-1) and 2^ 53-1.
Here we incidentally talk about the problem of accuracy loss, 0.1 + 0.2 is not accurate. Floating point numbers, which are infinite when expressed in binary and are at most 53 bits, must be truncated, resulting in errors. The easiest way to solve this problem is to scale it up to a certain number of times and then scale it down again. Maybe there’s a better way to do it.
There are four other numeric bases, some of which are the final form of our communication with computers:
The four numeric bases are decimal, hexadecimal, octal and binary.
- Decimal: a number without a leading 0. And this is what we use a lot
- Octal: Has a prefix
0o
or0O
Or a number that has a leading 0 and uses only the eight Arabic numerals from 0 to 7. - Hexadecimal: indicates a prefix
0x
or0X
The numerical values. - Binary: prefix
0b
or0B
The numerical values.
Here are some examples.
/ / decimal
123456789
0
// binary: prefix 0b, 0b
0b10000000000000000000000000000000 / / 2147483648
0b01111111100000000000000000000000 / / 2139095040
0B00000000011111111111111111111111 / / 8388607
// octal: prefix 0o, 0o (previously supported prefix 0)
0o755 / / 493
0o644 / / 420
// Hexadecimal: prefix 0x, 0x
0xFFFFFFFFFFFFFFFFF / / 295147905179352830000
0x123456789ABCDEF / / 81985529216486900
0XA / / 10
Copy the code
So much for JavaScript numbers. For more information, visit Baidu Wikipedia
1.2. An operation
The W3C explains javasCript bitwise operations like this:
JavaScript uses 32-bit bitwise operands
JavaScript stores numbers as 64-bit floating-point numbers, but all bitwise operations are performed as 32-bit binaries.
JavaScript converts the number to a 32-bit signed integer before performing a bit operation.
When the bitwise operation is performed, the result is converted back to a 64-bit JavaScript number.
To put it simply: bitwise operators operate on their operands as a sequence of 32-bit bits (zeros and ones) and return standard JavaScript values.
The bit operators in JavaScript have
The operator | usage | describe |
---|---|---|
Bitwise AND (AND) | a & b | For each bit, the result is 1 only if the corresponding bit bits of both operands are 1; otherwise, it is 0. |
Bitwise OR (OR) | a | b | For each bit, the result is 1 if there is at least one 1 in the corresponding bit of the two operands, and 0 otherwise. |
Bitwise XOR | a ^ b | For each bit, the result is 1 if there is one and only one 1 in the corresponding bit of the two operands, and 0 otherwise. |
Bitwise NOT | ~a | Invert the operand’s bits, i.e., 0 becomes 1,1 becomes 0. |
Left shift | a << b | Shift the binary form of A to the left by b (< 32) bits, filling the right with zeros. |
There’s a sign shift to the right | a >> b | Move the binary representation of A to the right by b (< 32) bits, discarding the removed bits. |
Unsigned shift to the right | a >>> b | Move the binary representation of A to the right by b (< 32) bits, discard the removed bits, and fill in the left side with zeros. |
Here are some examples to deepen your understanding of para-bit operations.
1.2.1 Bitwise and (&)
The && operator we all know that the result is true only if both are true. The value 1 is returned only if the value of both numbers is 1. For example, bitwise and operations for 1 and 3:
0001
& 0011
---------
0001
Copy the code
The result is 1 only if the corresponding number is 1, and all other values are 0. To determine whether a number is odd or even, we find the remainder:
function assert(n) {
if (n % 2= = =1) {
console.log("N is odd.");
} else {
console.log("N is even.");
}
}
assert(3); // "n is odd"
Copy the code
We can also do the bitwise & operation with a number and 1, which is faster:
function assert(n) {
if (n & 1) {
console.log("N is odd.");
} else {
console.log("N is even.");
}
}
assert(3); // "n is odd"
Copy the code
Here is the bit operation:
1 = 0001
3 = 0011-- -- -- -- -- -- -- -- & =0001
Copy the code
The last bit of an odd binary must be 1, and only the last bit of 1 must be 1. The result of the bit-& operation must be only the last bit of 1. The last digit of an even binary representation is 0, and the bitwise & operation with 1 results in all digits being 0.
1.2.2 bitwise or (|)
| and | | operators of truth is the same, as long as there is a number in the two Numbers is 1, the result is 1, the other is 0.
0001
| 0011
---------
0011
Copy the code
To round down a floating point number, we use the following method:
var num = Math.floor(1.1); / / 1
Copy the code
We can also use bitwise operations to complete:
var num = 1.1 | 0; / / 1
Copy the code
Floating-point numbers do not support bit operations, so 1.1 is converted to the integer 1 before the bit operation, as if the integer were rounded down. So 1 | 0 the result is 1.
1.2.3 Bitwise Non-(~)
Bitwise is the inverse of binary:
var num = 1; / / binary 00000000000000000000000000000001
var num1 = ~num; / / binary 11111111111111111111111111111110
Copy the code
As we know, numbers in JS are signed by default. The highest digit of the signed 32-bit binary, the first digit, represents plus and minus, 1 represents a negative number, and 0 represents an integer. 11111111111111111111111111111110 how much it? The highest bit, 1, represents a negative number, and the binary of a negative number is converted to decimal: the sign bit remains the same, and the other bits are incremented by 1. After 10000000000000000000000000000001, add 1 to 10000000000000000000000000000010, after decimal was 2.
1.2.4 Bitwise XOR (^)
Returns 1 in bitwise or if there is only one 1 in two numbers, or 0 in other cases.
0001
^ 0011
---------
0010
Copy the code
The bitwise xor operation between the number and the number itself yields 0, and since every two corresponding numbers are the same, 0 is returned.
We often need to swap two numbers:
var num1 = 1, num2 = 2, temp;
temp = num1;
num1 = num2; / / 2
num2 = temp; / / 1
Copy the code
If you want to be pushy, you can do this:
var num1 = 1, num2 = 2;
num1 = [num2, num2 = num1][0];
console.log(num1); / / 2
console.log(num2); / / 1
Copy the code
If you want to make it more stable, you can do this:
var num1 = 1, num2 = 2;
num1 ^= num2; // num1 = num1 ^ num2 = 1 ^ 2 = 3
num2 ^= num1; // num2 = num2 ^ (num1 ^ num2) = 2 ^ (1 ^ 2) = 1
num1 ^= num2; // num1 = num1 ^ num2 = 3 ^ 1 = 2
console.log(num1); / / 2
console.log(num2); / / 1
Copy the code
It’s over but it’s not over
There are a lot of things, so we will stop here for the moment. It is not over yet, and we will continue to talk about it in the next one. Interested partners can take note.
Creation is not easy, please support more, little like, little attention and collection, is my biggest support, thank you for watching my article, here is a small cut picture son _ ridiculous