Copy Code code as follows:
All unary operators, such as the ~ operator, calculate the value of an expression according to the following rules:
Copy Code code as follows:
1. If applied to an undefined expression or null expression, a run-time error is raised.
2, convert the object to a string.
3, if possible, converts a string to a number. Otherwise, a run-time error is raised.
4, the Boolean value is treated as a number (if False, 0, or 1 if true).
Operator will be applied to the result number.
The ~ operator looks at the value of the binary representation of the expression and performs a bitwise NON operation.
If any one in the expression is 1, the bit in the result becomes 0. If any one in the expression is 0, the bit in the result becomes 1.
The following example illustrates the use of the bitwise NON (~) operator, which contains a binary that represents a decimal number, and if you are unfamiliar with this, read the decimal negative conversion to binary, octal, hex.
Copy Code code as follows:
var temp = ~5;
/*
52 in 101, filling up 32 bits
00000000000000000000000000000101
Reverse by position
11111111111111111111111111111010
Since the 32-bit first is 1, this is a negative number, which converts the binary to a negative number, which requires an inverse code
00000000000000000000000000000101
After that, another +1
00000000000000000000000000000110
Convert to decimal 6, plus symbol to negative-6
*/
Alert (temp);
Pop-up "-6"