Home >Web Front-end >JS Tutorial >Detailed explanation of JS operator '!~'
This article mainly introduces in detail the javascriptoperator"!~", the most basic and easiest thing to ignore. After working for several years, even the basics are gone. When people ask, they will laugh generously.
It’s almost the New Year’s holiday, and I’m finally free. I browse various technical articles every day, and this state is great.
I read an article about js in the afternoon, and the following paragraph caught my attention.
The code is as follows:
(function () { var names = []; return function (name) { addName(name); } function addName(name) { if (!~names.indexOf(name))//如果存在则不添加 names.push(name); console.log(names);// ["linkFly"] } }())('linkFly');
What does the operator "!~" in if (!~names.indexOf(name)) mean? If you don’t understand, start with ~.
The test can show that the result value has this pattern - (X+1)
Search , and some articles only miss one sentence: negate the binary digit.
Literally, here it is expressed in eight-digit binary: 3=00000011, then ~3=11111100, it is wrong to apply the above formula.
The above explanation is still too abstract and not concrete. In fact, this involves the knowledge of original code, inverse code, and complement code.
Original code
The highest bit of the original code representation is the sign bit. This bit is 0 for positive numbers and 1 for negative numbers. The remaining bits represent the absolute value of the number.
Inverse code
For a signed number, the inverse code of a positive number is the same as its original code; the inverse code of a negative number is the bitwise inversion of all bits of the original code except the sign bit. The complement code is often used as an intermediate form in the process of finding the complement code.
Complement code
The complement code of a positive number is the same as its original code and complement code; the complement code of a negative number is obtained by inverting all bits of its original code except the sign bit and adding 1 to the last bit, which is Add 1 to the number's complement. Numbers in computers are generally represented in two's complement form. In the complement code, (-128)D is used instead of (-0)D. Note that: (-128)D has no corresponding original code and complemented code, (-128)D = (1000,0000)B.
Complement operation
The complement operation does not consider the sign bit, it is obtained by inverting each bit of its original code and adding 1 to the last bit. The complement of a number is the complement of its opposite.
Take the example of the author's article and understand that
~ means bitwise negation. Negation means that if it is 00111, it becomes 11000 (bitwise negation)
## The binary representation of #57 is (1 byte): 00111001The binary representation after bitwise inversion (~57): 11000110 This representation is decimal: -70
This is a negative number and a signed number , negative numbers should be represented by their complement in computers: complement = bitwise inversion after the sign bit and then adding 1.
So -70 (11000110) after bitwise inversion of the sign bit is (10111001) plus 1 is (10111010)
converted to decimal: -58
Therefore ~57=-58
The above is the detailed content of Detailed explanation of JS operator '!~'. For more information, please follow other related articles on the PHP Chinese website!