How to use JavaScript bitwise non-operator _ basics

Source: Internet
Author: User
Tags bitwise
~: Bitwise non-operator is represented by a wavy line (~), and the result of performing a bitwise non is the inverse code of the return value.
Copy Code code as follows:

var num1 = 3; My lucky number is 3.
var num2 = ~ (NUM1);
Console.log (num2)//"-4"
var num3 =-3;
var num4 = ~ (NUM3);
Console.log (NUM4)//"2"
Console.log (~ (0))//"-1"

Yes, now we know the principle of the ~ operator. Are you happy? Not happy, although this chapter, I have seen many times ... Because I have never used, is really ashamed ah. Where do you think this operator can be used? Well... Meditate and put a colleague's code:
Copy Code code as follows:

if (~item[search_key].tolowercase (). IndexOf (query)) {
_results.push (item);
}

Code:
Copy Code code as follows:

if (str.indexof (query)!=-1) or if (str.indexof (query) >= 0)

Principle Analysis:
The last value from Str.indexof (query) is no more than two kinds:
1. str contains the query string, the value is 0 or positive integer, at this time:!! (~str.indexof (query) = = = True (or this converts Boolean (~str.indexof (query) = = = True)
2. SRT does not contain the query string, the value is-1, at this time:!! (~str.indexof (query)) = = False
So by adding a ~ it will be good to judge the results of the indexof query. Fresh and incomparable, never had the dandruff of trouble. Ha ha!
Finally, we analyze the efficiency, the impression of the median operation efficiency should be compared to the high operator. To segment code:
Copy Code code as follows:

var str = "Hutaoer GO Go"!!!!! My lucky number is 33!! ";
var query = 33;
var timeStart1 = new Date ()-0;
for (var i = 0; i < 100000000; i++) {
~str.indexof (query)
}
var timeEnd1 = new Date ()-0;
Console.log (' ~ Cost time: ' + (TIMEEND1-TIMESTART1));
~ Cost time:9954 Cycle Number: 10000000
~ Cost time:104 Cycle Number: 100000
var timeStart2 = new Date ()-0;
for (var j = 0; J < 100000000; J + +) {
Str.indexof (query) >= 0
}
var timeEnd2 = new Date ()-0;
Console.log (' >= cost Time: ' + (TIMEEND2-TIMESTART2));
>= cost time:10120 cycle number: 10000000

Program Update: The original test code on the split line is unchanged. The code is as follows:
Copy Code code as follows:

var str = "Hutaoer GO Go"!!!!! My lucky number is 33!! ";
var query = 33;
var timeStart1 = new Date ()-0;
for (var i = 0; i < 1000000; i++) {
~str.indexof (query)
}
var timeEnd1 = new Date ()-0;
Console.log (' ~ Cost time: ' + (TIMEEND1-TIMESTART1));
Cycle 1 million times 127ms
var timeStart2 = new Date ()-0;
for (var j = 0; J < 1000000; J + +) {
Str.indexof (query) >= 0
}
var timeEnd2 = new Date ()-0;
Console.log (' >= cost Time: ' + (TIMEEND2-TIMESTART2));
Cycle 1 million times 101ms
var timeStart3 = new Date ()-0;
for (var k = 0; k < 1000000; k++) {
Boolean (~str.indexof (query))
}
var timeEnd3 = new Date ()-0;
Console.log (' Add Boolean time: ' + (TIMEEND3-TIMESTART3));
Cycle 1 million times 129ms
var timeStart4 = new Date ()-0;
for (var k = 0; k < 1000000; k++) {
!! (~str.indexof (query))
}
var timeEnd4 = new Date ()-0;
Console.log (' Add!! cost time: ' + (TIMEEND4-TIMESTART4));
Cycle 10 million times 103ms

In fact, for a single operation itself, the same, but in the cycle is too large, such as more than 10 million times, efficiency will have some gaps.
"Update 2013.10.27 17:28" through the modified test, we can find that the "bitwise not" of this method may not be the most efficient, the best performance is actually my usual way of writing, using the comparison operator. It really surprises me. Sometimes, people tend to be confused by common sense and appearances, but they may have different discoveries or other results when they try it themselves. Today, I learned the lesson.
In the comments, the students are opposed to this unusual way of writing, after all, these skills may be confusing to the students reading the code. If you don't know the principle, it's even confusing. Perhaps it would be a better choice to use some simple logic and common operators directly? What do you think?
So when you write code, you can use any kind of writing. But hopefully we can keep these techniques in mind and the key moments may be useful.

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.