Home >Web Front-end >JS Tutorial >How to use the bitwise NOT operator in JavaScript_Basic knowledge

How to use the bitwise NOT operator in JavaScript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:15:191187browse

~: The bitwise NOT operator is represented by a tilde (~). The result of performing the bitwise NOT operation is the complement of the returned value.

Copy code The 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"

Yes, now we know the principle of ~ operator . Are you happy? . . . Not happy, although I have read this chapter many times. . . Because I have never used it, I am really ashamed. Where do you think this operator can be used? kindness. . . Think for a moment and put a piece of code from a colleague:
Copy the code The code is as follows:

if (~ item[search_key].toLowerCase().indexOf(query)) {
                                                                                                                                                                                                           . 🎜>
Copy code

The 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. str contains query characters 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 srt does not contain the query string, the value is -1. At this time: !!(~str.indexOf(query)) === false
So by adding a ~, indexOf can be well adjusted The query results are judged. It's so refreshing and I no longer have the trouble of dandruff. . Ha ha!
Finally, let’s analyze the efficiency. The impression is that the efficiency of bitwise operations should be higher than that of operators. Here is a piece of code:



Copy code


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 < 100000000; i ) {        ~str.indexOf(query)    }
    var timeEnd1 = new Date() - 0;
    console.log('~ cost time:' (timeEnd1 - timeStart1));
// ~ cost time:9954 Number of loops: 10000000
// ~ cost time:104 Number of 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 Number of loops: 10000000


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




Copy the code

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));
    //  循环1000000次  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));
    // 循环1000000次 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));
    // 循环1000000次 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));
    // 循环10000000次 103ms
   

其实,对于一次运算本身来说,相差无几,只是在循环次数过大,比如超过了10000000次,效率才会有一些差距。
【更新 2013.10.27 17:28】通过修改后的测试,我们可以发现,“按位非”这中写法也许并非是效率最高的,表现最好的居然是我以前常用的写法,采用比较运算符。这确实让我很吃惊。有时候,人往往容易被常识,表象所迷惑,但亲自去尝试后,或许会有不一样的发现或得出其他的结果。今天,我算吸取教训了。
在评论中,同学们都比较反对这种非常见的写法,毕竟这些技巧可能会给阅读代码的同学造成困扰。如果不知道原理的话,甚至让人费解。或许,直接用一些简单的逻辑和常见的运算符,会是更好的选择?你们觉得呢?
因此平时写代码的时候,用哪种写法都可以。但是希望我们能将这些技巧记住,关键时刻或许就能派上用场。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn