Studying relevant information on bitwise operations in JavaScript, I really don’t understand the inverted bitwise operation. I don’t understand why the inverted bitwise operation is not the maximum number - the current value, but ~8=-9, ~-8=7?
Because ~8 is exactly how -9 is represented in computers. . . . 32-bit unsigned integer can represent the positive integer range of 0 ~ 2^32-1, which can represent 2^32 integers. 0 ~ 2^32-1的正整数范围,这样可以表示2^32个整数。 当作为有符号数的时候,不是把最高的比特位作为符号位,即 -1 不是直接把000..001 的最高bit置为 1 ,而是使用其 -1 + 2^32 = 2^32-1 对应的二进制数表示。这种形式叫做补码。一种最快的求负数补码的方式是,其绝对值的二进制,从低位开始,遇到的第一个 1 之前(包括这个1)不变,其他的 1 变 0, 0 变 1。 比如 -4 的补码是, 4 -> 00...0100 -> 11...1100When used as a signed number, instead of using the highest bit as the sign bit, that is, -1, instead of directly setting the highest bit of 000..001 to 1, Instead, use its corresponding binary number representation of -1 + 2^32 = 2^32-1. This form is called two's complement. One of the fastest ways to find the complement of a negative number is to use the binary value of its absolute value, starting from the low bit, before the first 1 encountered (including this 1), unchanged, and other 1s become 0, and 0 becomes 1. For example, the complement of -4 is, 4 -> 00...0100 -> 11...1100
The advantage of this is that it reduces the operation rules. For addition and subtraction, the computer does not have to distinguish whether it is signed or not. For example, 4-bit shaping. There is a -5 + 4 = -1 二进制表示是 1011 + 0100 = 1111,而无符号的11+4=15二进制形式也是1011 + 0100 = 1111 。如果用1101 表示-5那么有符号加法就是1101 + 0100 = 1111 symbol, which is inconvenient for people and also inconvenient for computers.
You are right, the inverse is 最大的值-当前的值 这是对于无符号整数来说的。 只是除了>>> In addition, the return value of JS bit operators is a signed 32-bit integer.
Because ~8 is exactly how -9 is represented in computers. . . .
32-bit unsigned integer can represent the positive integer range of
0 ~ 2^32-1
, which can represent2^32
integers.0 ~ 2^32-1
的正整数范围,这样可以表示2^32
个整数。当作为有符号数的时候,不是把最高的比特位作为符号位,即
-1
不是直接把000..001
的最高bit置为 1 ,而是使用其-1 + 2^32
=2^32-1
对应的二进制数表示。这种形式叫做补码。一种最快的求负数补码的方式是,其绝对值的二进制,从低位开始,遇到的第一个 1 之前(包括这个1)不变,其他的 1 变 0, 0 变 1。 比如 -4 的补码是, 4 ->00...0100
->11...1100
When used as a signed number, instead of using the highest bit as the sign bit, that is,-1
, instead of directly setting the highest bit of000..001
to 1, Instead, use its corresponding binary number representation of-1 + 2^32
=2^32-1
. This form is called two's complement. One of the fastest ways to find the complement of a negative number is to use the binary value of its absolute value, starting from the low bit, before the first 1 encountered (including this 1), unchanged, and other 1s become 0, and 0 becomes 1. For example, the complement of -4 is, 4 ->00...0100
->11...1100
The advantage of this is that it reduces the operation rules. For addition and subtraction, the computer does not have to distinguish whether it is signed or not. For example, 4-bit shaping. There is a
-5 + 4 = -1
二进制表示是1011 + 0100 = 1111
,而无符号的11+4=15
二进制形式也是1011 + 0100 = 1111
。如果用1101
表示-5
那么有符号加法就是1101 + 0100 = 1111
symbol, which is inconvenient for people and also inconvenient for computers.You are right, the inverse is
最大的值-当前的值
这是对于无符号整数来说的。 只是除了>>>
In addition, the return value of JS bit operators is a signed 32-bit integer.Reverse? Bitwise NOT, the result of performing bitwise NOT is to return the complement of the value.
Because the bitwise negation here is not the true bitwise negation, but
补码运算
.Please refer to this question for details: How to understand bitwise negation in js?
The inversion operation will also invert the sign bit. For details, see: "JavaScript Advanced Programming" 3.5.2 Bit Operations