Use of bitwise non-operators in javascript

Source: Internet
Author: User

~ : The bitwise non-operator is composed of a wavy line (~) The result of executing a non-bitwise operation is the reverse code of the returned value.
Copy codeThe Code is 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"

That's right. Now we know ~ The principle of the operator. Are you happy ?... Unhappy. Although I have read this chapter many times... Because I have never used it. It's really embarrassing. Where do you think this operator can be used? Well... Think twice and put a piece of code from a colleague:
Copy codeThe Code is as follows:
If (~ Item [search_key]. toLowerCase (). indexOf (query )){
_ Results. push (item );
}

Code:
Copy codeThe Code is as follows:
If (str. indexOf (query )! =-1) or if (str. indexOf (query)> = 0)

Principle Analysis:
The final value obtained through str. indexOf (query) is nothing more than two types:
1. If str contains a query string, the value is 0 or a positive integer. At this time :!! (~ Str. indexOf (query) = true (or convert Boolean (~ Str. indexOf (query) === true)
2. If the srt does not contain a query string, the value is-1. At this time :!! (~ Str. indexOf (query) = false
Therefore, by adding ~ You can make a good judgment on the indexOf query results. It's refreshing, and there's no headache .. Haha!
Finally, let's analyze the efficiency. The efficiency of the impression center operation should be higher than that of the operator. Section code:
Copy codeThe Code is as follows:
Var str = "huaoer 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 loops: 10000000
//~ Cost time: 104 loops: 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 times: 10000000

Program update: the original test code remains unchanged on the split line. The Code is as follows:
Copy codeThe Code is as follows:
Var str = "huaoer 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 1000000 times 127 ms
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 1000000 times 101 ms
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 cost time: '+ (timeEnd3-timeStart3 ));
// Cycle 1000000 times 129 ms
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 10000000 times 103 ms

In fact, for an operation itself, there is almost no difference in efficiency, but the number of loops is too large. For example, if it exceeds 10000000 times, there will be some gaps in efficiency.
[Updated at 17:3.10.27] through the modified test, we can find that the writing of "by bit" may not be the most efficient, the best performance was my previous commonly used writing method, using comparison operators. This really surprised me. Sometimes, people are often confused by common sense and appearance, but after trying it, they may find different results or get other results. Today, I learned a lesson.
In the comments, the students are opposed to this very insightful writing method. After all, these skills may cause troubles for the students who read the code. If you do not know the principle, it is even confusing. Maybe it is better to simply use some simple logic and common operators? What do you think?
Therefore, you can use either method to write code. But we hope we can remember these skills and the key moments may come in handy.

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.