Today I will introduce the double-bit NOT operation in JavaScript ~~
. ~~
The operator is a powerful and often overlooked feature. It provides a fast, concise way to manipulate numbers and perform type conversions, and can often be used for mathematical calculations and type conversions. Let’s first understand ~~
the basic concepts of and some of its application scenarios.
~~
Operator introduction
In JavaScript, ~~
it is a double use of the bitwise NOT operator. The bitwise NOT operator ( ~
) inverts all the bits of its operand (that is, 1 becomes 0 and 0 becomes 1). When you use (eg) twice in a row ~~
, it inverts the bits again, thus effectively returning the original value.
Given an expression ~foo
, its final effect is -(foo + 1)
. So a double bit NOT operation ~~foo
will result -(-(foo + 1) + 1)
. This only works with integers , given all possible operands, and ~~
the equivalent real expression might be like this, but the real internal calculation is certainly not like this:
typeof foo === 'number' && ! isNaN (foo) && foo !== Infinity ? foo > 0 ? Math . floor (foo) : Math . ceil (foo) : 0 ;
Here are some examples of double-bit NOT operations:
~~ null ; // => 0 ~~ undefined ; // => 0 ~~ 0 ; // => 0 ~~{}; // => 0 ~~[]; // => 0 ~~( 1 / 0 ); // => 0 ~~ false ; // => 0 ~~ true ; // => 1 ~~ 1.2543 ; // => 1 ~~ 4.9 ; // => 4 ~~(- 2.999 ); // => -2
When you’re dealing with positive numbers, ~~
‘s functionality makes it Math.floor
a better alternative to , although it’s not as readable, it’s faster and takes up fewer characters.
Application scenarios
- Convert floating point number to integer :
~~
Can be used to quickly convert floating point numbers to integers. This conversion is similar toMath.floor()
, but more concise and in some cases more performant.- Example:
~~4.9
The result is4
.
- Handle non-numeric values :
- When using a non-numeric value (such as a string)
~~
, JavaScript will first try to convert it to a number. If the conversion fails, the result is0
. - Example:
~~"hello"
The result is0
.
- When using a non-numeric value (such as a string)
- Data cleaning and validation :
- When dealing with user input or undefined data,
~~
this ensures that you end up with an integer, or if conversion is impossible0
.
- When dealing with user input or undefined data,
- Performance optimization :
- In some performance-sensitive applications,
~~
as a fast integer conversion method, it can provide faster performance than the standard method.
- In some performance-sensitive applications,
in conclusion
~~
The operator method executes very quickly, which is very suitable when you perform millions of such operations. The speed is obviously better than other methods, but the readability of the code is relatively poor. One more thing to note, the bitwise double NOT operator method only works with 32-bit integers, ie (2**31)-1 = 2147483647
. So for any 2147483647
number above, the bitwise operator ( ~~
) will give wrong result, so it is recommended in this case Math.floor()
.