The New Year holiday is coming soon, and I finally have free time. I visit various technical articles every day, and this is in a great state.
I read an article about js in the afternoon, and there is a paragraph like this that caught my attention.
The code copy 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, start with ~ first.
Tests can show that the result value has this rule - (X+1)
After searching, some articles only lose one sentence: turn it inverted by binary bit
From the literal meaning, here we use eight-digit binary representation: 3=00000011, then ~3=11111100, the above formula is wrong.
The above explanation is still too abstract and not specific. In fact, this involves the knowledge of original code, reverse code, and complement code.
Original code
The highest bit of the original code representation is the sign bit, which is 0 to indicate a positive number and 1 to indicate a negative number. 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 bit inverse of the bits except the sign bit. Inverse code is often used as an intermediate form in the process of finding complement code.
Complement
The complement of a positive number is the same as its original code and inverse code; the complement of a negative number is to invert its original code except the sign bit, and add 1 to the last bit, that is, add 1 to the complement of the number. Numbers in computers are generally expressed in complement form. In the complement code, (-128)D is used instead of (-0)D. Note: (-128)D does not have the corresponding original code and inverse code, (-128)D = (1000,000)B.
Find complementary operations
The complement operation does not consider the sign bits, but inverts the original bits of its original code, and adds 1 to the last bit. The complement of a number is the complement of the opposite number.
Take the author's article example and understand it
~ means bitwise inversion. If it is 00111, it becomes 11000 (bitwise inversion)
The binary representation of 57 is (1 byte): 00111001
Binary after bitwise inversion (~57): 11000110 This is expressed as decimal:-70
This is a negative number, a signed number. Negative numbers should be expressed in the computer using their complement code: complement = the symbol bit is inverted and then added 1.
Therefore, after the -70(11000110) sign bit is inverted after bitwise, it is (10111001) and then add 1, it is (10111010)
Change to decimal system:-58
Therefore ~57=-58
At this point, I finally figured it out. Although the summary formula can quickly draw results, it cannot explain why. As technicians, we like to study and go deep into details.
Give time:
The foundation is the cornerstone of all the upper levels. If you devote yourself to practicing Taoism, the road is long.
The above is the entire content of this article. I hope everyone can gain something.