This is the fourth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
Have you ever seen a variable with a special symbol like ~/ ** before it and wondered what it meant? Indeed! There are many special symbols in JavaScript, we are particularly familiar with addition, subtraction, multiplication and division, and these special symbols because of the use of less, but not familiar with, when looking at some source code or big guy’s code some confused, what is this operation? ! Let’s learn about bit operators!
Bit operators for JavaScript special symbols
For example, this ** operator is also used less, this ** is a power operation.
And <<, >> are those two familiar?
The left-shift operator (<<) moves the first operand to the left by the specified number of digits. The excess left digit is cleared and the right digit is zeroed.
const a = 5; / / 00000000000000000000000000000101
const b = 2; / / 00000000000000000000000000000010
console.log(a << b); / / 00000000000000000000000000010100 = = > 20
Copy the code
In some languages, it might be customary to use left and right shifts instead of multiplication and division, but to use them in JavaScript, you need to discard the highest 22 significant digits in a numeric value. Sometimes you can ignore 22, but a lot of times you can’t ignore 22. All used less in JavaScript.
What do bitwise operators know?
Here the two should be familiar with, and operations (&) and/or operations (|), and (^) of an exclusive or operation? , don’t pay attention to and && and | | remember confused!
# | Bit operation symbol | Role to explain |
---|---|---|
1 | & |
With the operation |
2 | | |
Or operation |
3 | ^ |
Exclusive or operation |
4 | << |
Shift to the left |
5 | >>> |
Moves to the right |
6 | >> |
The signed expansion moves right |
7 | ~ |
Not operation |
And of course there’s only one unary operator in JavaScript, ~, which stands for “not”,
When a bit operation is performed in JavaScript, it is first converted to a 32-bit signed integer (int) and evaluated, and then converted to a JavaScript numeric type when the result is obtained.
show me code
let result1 = 1 & 1
let result2 = 1 & 2
let result3 = 1 | 1
let result4 = 1 | 2
document.write(result1, result2, result3, result4) / / 1013
console.log(result1, result2, result3, result4) // 1 0 1 3
Copy the code
Learn your persistence, come on
Learn a knowledge point, on the basis of understanding, record, accumulate over a long period of time, I believe we can become a god (dream always have ~)!