Home  >  Q&A  >  body text

Bit Operations - Why ~8=-9, ~-8=7 in javascript?

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?

滿天的星座滿天的星座2731 days ago983

reply all(5)I'll reply

  • PHPz

    PHPz2017-05-18 11:01:22

    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.

    function toUint32(x) {return x>>>0;}
    function toInt32(x) { return x>>0;}
    
    MaxUint32 = toUint32(-1);// -1 的二进制表示和 2^32 - 1 一样(32位整形来说)
    
    console.log(MaxUint32) // 4294967295
    
    console.log(8 + toUint32(~8) === MaxUint32)  // true
    console.log(7 + toUint32(~7) === MaxUint32)  // true
    
    //  下面几个与本问题无关,就当是扩展了,自己试试输出是什么。
    console.log(MaxUint32 + 1)
    console.log(toUint32(MaxUint32+1))
    console.log(toUint32(MaxUint32+2))

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-18 11:01:22

    Reverse? Bitwise NOT, the result of performing bitwise NOT is to return the complement of the value.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-18 11:01:22

    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?

    reply
    0
  • 阿神

    阿神2017-05-18 11:01:22

    The inversion operation will also invert the sign bit. For details, see: "JavaScript Advanced Programming" 3.5.2 Bit Operations

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-18 11:01:22

    ~8=-9
    8的二进制 11000 =》第一位是符号位,正数1,负数0
    按位取反 =》所有位取反,再取补码
    11000 取反=》 00111 补码(负数的补码,符号位不动,其它取反+1)=》 01000+1 =》01001(-9)
    
    ~-8=7?
    -8的二进制 01000 =》第一位是符号位,正数1,负数0
    按位取反 =》所有位取反,再取补码
    01000 取反=》 10111 补码(正数的补码是其本身)=》 10111(7)
    

    reply
    0
  • Cancelreply