Home > Web Front-end > JS Tutorial > Detailed explanation of javascript operator '!~'_Basic knowledge

Detailed explanation of javascript operator '!~'_Basic knowledge

WBOY
Release: 2016-05-16 16:15:01
Original
1475 people have browsed it

It’s almost the Chinese New Year holiday, and I’m finally free. Browsing through various technical articles every day, this state is great.

I read an article about js in the afternoon, and the following paragraph caught my attention.

Copy code The code is as follows:

(function () {
var names = [];
Return function (name) {
          addName(name);
}
Function addName(name) {
If (!~names.indexOf(name))//If it exists, do not add
             names.push(name);
console.log(names);// ["linkFly"]
}
}())('linkFly');

What does the operator "!~" in if (!~names.indexOf(name)) mean? If you don’t understand, let’s start with ~.

The test can show that the result value has this pattern - (X 1)

After searching, some articles only lost 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 one's complement of a positive number is the same as its original code; the one's complement 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 of a positive number is the same as its original code and complement; the complement 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 the complement of the number plus 1. 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 author’s article as an example to understand

~ means bitwise negation. Negation means that if it is 00111, it becomes 11000 (bitwise negation)

The binary representation of 57 is (1 byte): 00111001
The binary representation after bitwise negation (~57): 11000110 This is expressed in decimal: -70
This is a negative number, which is a signed number. Negative numbers are represented by their complement in computers: complement = after the sign bit, the bitwise inversion is added and 1 is added.
Therefore, after the sign bit of -70 (11000110) is inverted bitwise, it is (10111001), and adding 1 is (10111010)
Convert to decimal:-58
Therefore~57=-58

Now I finally understand it. Although the summarized formula can quickly produce results, it cannot explain why. As technical people, we like to delve into the details.

Sigh time:

The foundation is the cornerstone of everything above. If you devote yourself to cultivating the Tao, the road will be long.

The above is the entire content of this article, I hope you can gain something from it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template