Use of javascript bitwise non operators

  • 2020-03-29 23:43:20
  • OfStack

~ : the bitwise nonoperator is represented by a wavy line (~). The result of performing bitwise nonoperators is the inverse of the returned value.

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 how the ~ operator works. Happy? . Not happy, although I have read this chapter many times... Because I have never used it, I am ashamed of it. Where do you think this operator can be used? Um... Meditate. Put a piece of code from a colleague:

if (~item[search_key].toLowerCase().indexOf(query)) {
                        _results.push(item);
 }

Code:

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

Principle analysis:
There are no more than two kinds of values obtained from STR. IndexOf (query) :
1. STR contains the query string, and the value is either 0 or a positive integer. (~ STR. IndexOf (query)) === true(or convert Boolean(~ STR. IndexOf (query)) == true)
2. SRT does not contain the query string, then the value is -1, at this time:!! (~ STR. IndexOf (query)) = = = false
Therefore, by adding a ~, we can judge the query result of indexOf well. Very cool, from now on there is no dandruff trouble. Ha ha!
Finally, let's analyze the efficiency. I think the efficiency of bit operation should be higher than that of operator. To the code:

var str = "hutaoer go 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   Number of cycles: 10 million
    //~ cost time: 104   Number of cycles: 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   Number of cycles: 10 million

Program update: the original test code remains unchanged above the split line. The code is as follows:

    var str = "hutaoer go 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));
    //  Circulation 1000000 times & NBSP; 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));
    //Cycles 1000000 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 cost time:' + (timeEnd3 - timeStart3));
    //Cycles 1000000 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));
    //Loop 10000000 times 103ms
    

In fact, for an operation itself, the difference is the same, only in the number of cycles is too large, say more than 10 million times, there will be some difference in efficiency.
【 update 2013.10.27 17:28 】 through the modified test, we can find that "not by bit" may not be the most efficient way to write, the best performance is actually the way I used to write, using the comparison operator. It really surprised me. Sometimes, people are often easy to be confused by common sense, appearance, but the person to try, may have different findings or other results. Today, I learned my lesson.
In the comments, the students objected to this unusual way of writing, because these techniques might cause problems for the students reading the code. It's even confusing if you don't know how it works. Perhaps it would be better to just use some simple logic and common operators? What do you think?
So when you're writing code, you can write it either way. But hopefully we can keep these tips in mind so that we can use them at critical moments.

Related articles: