~ The operand is reversed, the meaning of two is two times to take the inverse operation, in fact, the original number is equal (the operand is 32 integer range)
Here's an example:
var n = -4.9//4.9n = ~n;console.log (n); // 3n = ~n;console.log (n); // 4
Example 2:
var n = 4.2//4.2n = ~n;console.log (n); // -5n = ~n;console.log (n); // 4
Example 3:
var n = 4//4n = ~n;console.log (n); // -5n = ~n;console.log (n); // 4
First, if a data is <0 when it tries to convert to 32 shaping data, it needs to be rounded up, such as -2.9->-2, if >0, rounding it down, such as:2.6->2;
If a data cannot be converted to a 32-bit binary representation, it is converted to NaN, and then to 1; for example ~{}/~nan ==-1;
Signed integers use 31-bit numeric values that represent integers, a 32nd digit symbol for integers, 0 for positive numbers, and 1 for negative numbers.
The values range from -2^31-2^31-1-2147483648 to 2147483647.
When JavaScript makes bit operations, it uses a 32-bit signed integer, which means that the result of its conversion is also a 32-bit signed integer.
A detailed explanation of the ~ operator:
Converts expression to a 32-bit signed integer, and then the bitwise 1 complement. That is, each bit of 0 in the result is set to 1, and each bit of 1 in the result is set to 0. The result is a signed 32-bit integer. This operator is also referred to as a complement operator or a bitwise complement operator.
For example, a hexadecimal value of 0x7777 is represented as a binary number:
0111011101110111
The binary number of the bitwise inverse of the hexadecimal value ~0x7777 is:
1000100010001000
In hexadecimal, it is 0x8888. Therefore, ~0x7777 is 0x8888.
Reference Address: https://zhidao.baidu.com/question/554279014.html?qq-pf-to=pcqq.c2c
Http://www.jb51.net/article/27852.htm
JS bitwise NON (~) operator and ~ ~ Operator's Comprehension analysis