-128 and 128 in 2 additions

In 2, the addition 0-127 is represented as 00000000 to 01111111. In the case of negative numbers, we invert all the bits in the unsigned representation and add 1 to get 2 additions.

(Link: http://en.wikipedia.org/wiki/Signed_number_representations#Two.27s_complement )

therefore -1 out of 2 additions will be:

  unsigned 1 = 00000001

  invert all bits = 11111110

  add 1 = 11111111

But for -128, if we follow the same steps:

  unsigned 128 = 10000000

  invert all bits = 01111111

  add 1 = 10000000

therefore -128 and 128 have the same representation in 2 additions additions? Why not a range of 2 padding for 8 bits set as -127 - 128? In short, why is -128 preferable to representing unsigned 128 using the same number of bits?

+8
binary twos-complement
source share
3 answers

There is no "128" in the signed byte. Range

  • 0 to 127: 128 values
  • -1 to -128: 128 values

A total of 256 values, i.e. 2 ^ 8.

Adding based on the comment (and re-reading the question)

0x80 could be considered -128 or +128. Wikipedia explanation is worth reading

Two additions of the minimum number in the range will not have the desired effect of negating the number.

For example, two -128 additions on an 8-bit system result in the same binary number. This is because a positive value of 128 cannot be represented with an 8-bit binary binary digit. Note that this is detected as an overflow condition, since the transfer was performed, but not from the most significant bit. This can lead to unexpected errors in the fact that an uncontrolled implementation of an absolute value can return a negative number in the case of a minimum negative value. The abs integer family in C usually has this behavior. This is also true for Java. In this case, the developer must decide whether to check the minimum negative value before calling the function.

The most negative number in two additions is sometimes called the "strange number" because it is the only exception. Although the number is an exception, it is a valid number in the regular two complement systems. All arithmetic operations work with it both as an operand and (if there was no overflow) the result.

In addition, the processor that propagates MSb (bit 7) to the right will have the right shift of the signed integer, which will be against simple logic if 0x80 is +128, because after just one shift we get 0xC0 , which is a negative number (-64) ... (while the right shift from a positive number usually cannot give a negative result).

+7
source share

-128 is preferred over 128 because of the symbol bit designation. In a representation with the number of characters, the most significant bit is considered as a sign bit. If this bit is 1, this number is negative. In 128 and -128 (10,000,000), this bit is 1, so it means -128, not 128.

See http://en.wikipedia.org/wiki/Sign_bit

+6
source share

To save MSB as sign bit

+1
source share

All Articles