Performs a bitwise non-(negation) operation on an expression.
result = ~ Expression
Parameters
Result
Any variable.
Expression
Any expression.
Note
All unary operators, such as the ~ operator, calculate the value of an expression according to the following rules:
• If applied to an undefined expression or null expression, a run-time error is raised.
• Converts an object to a string.
• Converts a string to a number, if possible. Otherwise, a run-time error is raised.
• The Boolean value is treated as a number (0 if False, 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 bit non (~) operator.
var temp = ~5;
The resulting value is-6, as shown in the following table.
An expression
Binary value (complement of 2)
Decimal value
5
00000000 00000000 00000000 00000101
5
~5
11111111 11111111 11111111 11111010
-6
The following example illustrates the use of the bit non (~) operator, which contains a binary that represents a decimal number, and if you are unfamiliar with this, read it first.
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"
Some about bitwise operations
There are 7 JavaScript bit operations
& | ^ ~ << >> >>>
by Bit and (&)
When the same bit of two digits returns 1 for 1 o'clock, otherwise it returns 0.
For example, the binary representation of a 1&2=0,1 as a 0001,2 is 0010, and the operation of the two returns 0000.
0001
0010
-------
0000
We're 0.
Bitwise OR (|)
When two numbers only have one for 1, it is 1 2|1=3
0010
0001
-------
0011
We got 3.
Bitwise NON (^)
Two operands corresponding bit is different when the corresponding bit of the result is 1, otherwise 9^6= 15
1001
0110
-------
1111
We got 15.
Bit non (~)
Bitwise non-operator "~" implements the bitwise negation operation of the operand, belonging to the monocular operator
"Bit non", as the name suggests, the operand is reversed by a bit. ~7=-8
0111
------==========> should have a symbol on the front, so it's 8.
1000
Move left (<<)
Left-shift operator "<<" to achieve the overall left to move the bottom of the function of 0, belong to the binocular operator
15<<2=60
0000 1111
0011 1100
Move left (>>)
Left-shift operator ">>" to achieve the overall left to move the bottom of the function of 0, belong to the binocular operator
15>>2=3
0000 1111
0000 0011