Javascript operator "!~ "Detailed explanation, javascript Operator
The Chinese New Year holiday is coming soon, and I am idle. I visited various technical articles every day. This is a good situation.
I read an article about js in the afternoon. The following section caught my attention.
Copy codeThe Code is as follows:
(Function (){
Var names = [];
Return function (name ){
AddName (name );
}
Function addName (name ){
If (!~ Names. indexOf (name) // if it exists, no
Names. push (name );
Console. log (names); // ["linkFly"]
}
} () ('Linkfly ');
If (!~ Operator in names. indexOf (name "!~ "What does it mean? I don't understand it ~ Start.
This rule can be obtained through testing-(X + 1)
Search, some articles only lose one sentence: reverse retrieval by binary bit
Literally, it is represented in eight-bit binary: 3 = 00000011, that ~ 3 = 11111100. The formula above is incorrect.
The above explanation is too abstract and not specific. In fact, this involves the knowledge of the source code, anti-code, and complement code.
Original code
The highest digit in the original code representation is the sign bit. If this digit is 0, it indicates a positive number and 1 indicates a negative number. The other bits represent the absolute values of the number.
Reverse code
For a signed number, the positive anticode is the same as the original code. For a negative number, the inverse code is bitwise except the symbol bit. The reverse code is usually used as an intermediate form in the Code completing process.
Complement
The positive complement is the same as the original and reverse code. The negative complement is used to reverse the original code except the symbol bit, and Add 1 to the last digit, that is, add 1 to the complement of this number. The number in a computer is generally expressed as a complement. (-128) D is used to replace (-0) D with (-128) D. Note: (-128) D does not have the corresponding source code and anti-code, (-) D =) b.
Complement operation
The complement operation does not consider the symbol bit. You can reverse the original code and Add 1 to the last bit. After performing a population operation on a number, the result is the complement of the opposite number.
Take the author's article as an example.
~ It means bitwise inversion. If the bitwise inversion is 00111, it is changed to 11000 (bitwise inversion)
The binary representation of 57 is (1 byte): 00111001
After bitwise inversion (~ 57) binary: 11000110 indicates decimal:-70
This is a negative number, indicating a signed number. The negative number must be indicated by its complement code in the computer. The complement code is equal to or greater than 1 after the bitwise result is obtained.
Therefore, after the-70 (11000110) symbol bit is reversed by bit, the value is (10111001) plus 1, and the value is (10111010)
To-58 in decimal format.
Therefore ~ 57 =-58
At this point, I finally figured it out. Although the summary formula can quickly produce results, it cannot explain why, as a technician, we like to study and go into details.
Exclamation time:
The foundation is the cornerstone of all upper layers, and the road is long.
The above is all the content of this article, and I hope you can get it.