JavaScript operator "!~" Details _ Basics

Source: Internet
Author: User
Tags bitwise

The New Year holiday, also finally idle down. It's a great state to visit every day in a variety of technical articles.

In the afternoon read the article about JS, which has the following such a paragraph caught my attention.

Copy Code code as follows:

(function () {
var names = [];
return function (name) {
AddName (name);
}
function AddName (name) {
if (!~names.indexof (name))//If present, do not add
Names.push (name);
Console.log (names);//["Linkfly"]
}
} ()) (' Linkfly ');

if (!~names.indexof (name)) in the operator "!~" what meaning, do not understand, start from ~.

Tests can result in a rule with this value-(x+1)

Search some, some articles only lose one sentence: press bits to take counter

Literally, here in eight-bit binary notation: 3=00000011, that ~3=11111100, the formula is not right.
The explanation above is still too abstract and not specific. In fact, this involves the original code, inverse code, complementary knowledge.

Original code
The original code notation is the highest bit of symbol bit, which is 0 for positive numbers and 1 for negative numbers. The remaining bits represent the absolute value of the number.
Anti-code
For a signed number, the inverse of a positive is the same as its original code, and a negative inverse code is a bitwise counter for any other than the symbol bit of its original code. The inverse code is often used to do the intermediate form in the complement process.
Complement
The complement of a positive number is the same as its original code and the inverse code; the complement of the negative is to the original code in addition to the symbol number of the counter, and at the bottom of the Add 1 to get, that is the complement of the numbers plus 1. The number in the computer is usually expressed in the complement form. In the complement of ( -128) d instead of ( -0) d, note: ( -128) d does not correspond to the original code and the inverse code, ( -128) d = (1000,0000) B.
To find the complement operation
The complement operation does not take into account the symbol bit, to its original code you take counter, and at the bottom add 1 to get. The complement of a number is obtained by the inverse number of the number.

Take the author's article example, understand

~ is to reverse the meaning of a bit, the reverse is if it is 00111, it becomes 11000 (bitwise reverse)

The binary representation of 57 is (1 bytes): 00111001
Bitwise reverse (~57) binary: 11000110 This representation is decimal:-70
This is a negative number, there are signed numbers, negative numbers in the computer to use its complement to represent: complement = symbol bit after the counter plus 1.
So the 70 (11000110) sign bit after the bitwise reverse is (10111001) plus 1 is (10111010)
Change to decimal:-58
So ~57=-58

It's finally figured out. Although the summary formula can quickly produce results, but can not explain why, as a technical person we like to delve into the details.

Exclamation time:

Foundation is the cornerstone of all the upper layer, with great concentration of monastic, long road.

The above is the entire content of this article, I hope you can have income.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.