result = ~ [Number]
All unary operators (such as the ~ operator) evaluate expressions according to the following rules:
1. If applied to an undefined expression or null expression, a runtime error is raised.
2. Convert the object to a string.
3. If possible, convert the string to a number. Otherwise, a runtime error will be thrown.
4. Boolean values are treated as numbers (0 if false; 1 if true).
The
operator will be applied to the resulting number.
The
~ operator looks at the value of the binary representation of an expression and performs a bitwise NOT operation.
If any bit in the expression is 1, that bit in the result becomes 0. If any bit in the expression is 0, that bit becomes 1 in the result.
The following example illustrates the usage of the bitwise NOT (~) operator, which includes binary representation of negative decimal numbers. If you are not familiar with this, please read " Conversion of negative decimal numbers into binary, octal, and hexadecimal 》.
var temp = ~5;
/*
5 Binary 101, fill up 32 bits
0000000000000000000000000000101
Negrate bitwise
111111111111111111111111111111010
Since the first 32 bits start with 1 , so this is a negative number, convert binary to a negative number, You need to first invert
0000000000000000000000000000101
, then 1
000000000000000000000000000000110
to convert to decimal 6, add the sign to become a negative number -6
*/
alert(temp);
// Pop up【-6】