Copy code code as follows:
result = ~ [Number]
All one -dollar component (such as ~ operator) calculate the value of the expression according to the following rules:
Copy code code as follows:
1. If it is applied to an unfarished expression or Null expression, it will cause a runtime error.
2. Convert the object to a string.
3. If possible, convert the string into numbers. Otherwise, it will cause errors during operation.
4. Boolean value is considered a number (if it is FALSE, it is 0; if it is true, it is 1).
The operator will be applied to the result number.
~ Calculation symbols View the value of the binary representation of the expression, and the execution bit is non -computing.
Any one in the expression is 1, then the bit in the result becomes 0. Any bit in the expression is 0, then the bit in the result becomes 1.
The following example explains the usage of the operator (~) operators, which contains binary indicating decimal negative numbers. If you are not familiar with this, please first understand the "decimal negative numbers convert to binary, octagonal, hexadecimal".
Copy code code as follows:
var test = ~ 5;
/*
5 binary 101, complement 32 -bit
0000000000000000000000000101
Bind by bit
11111111111111111111111111111010
Since the first one at the beginning of the 32 -bit is 1, this is a negative number, which converts binary into negative numbers.
0000000000000000000000000101
After that, then +1
000000000000000000000000000110
Converted to decimal to 6, plus the symbol becomes negative-6
*/
alert (temp);
// Poppy [-6]