1
votes

Finding 2's complement.

If a number is positive, then its representation is same as that of its signed repesentation

0101 ???? +5
0110 ???? +6
0111 ???? +7

If a number is negative, we convert first take one's complement of its signed representation and add one to it.

Like for -7 it is 0111+1=1000

for -128 it is one's complement of +128 and add one to it.signed representation of +128 is 010000000 (first zero is for sign). its ones complement is 101111111. adding one to i t gives 110000000 which is a nine bit numbers.

Hence we cannot store -128 in two's complement form????

1
You can represent -128 in a single byte as 2s complement, but you cannot represent +128 -- only up to +127.Hot Licks
@hot licks how -128 i hae shown here we cannot storeIshan Bansal
The representation of +128 in 8 bits is 10000000. One's complement is 01111111. Add one to get 10000000, the two's complement representation of -128. Notice it's the same as the representation of +128. That means we have to decide (systematically) whether 10000000 means +128 or -128. We choose to make it -128 so that the first bit is a sign bit.user253751
Yep, the standard convention for 2s complement numbers is that the high-order bit is the sign bit, so 10000000 is a negative number.Hot Licks
The representation of -7 as a 4-bit number is 1001. That's because the one's complement of 0111 is 1000, and then you add one to get 1001.user3386109

1 Answers

2
votes

An 8-bit value can represent the values 0-255 unsigned or -128 to +127 signed.

  • The 8-bit unsigned representation of +128 is 1000 0000 (drop the leading 0 from your question).
  • Its one's complement is 0111 1111.
  • Adding one to it gives 1000 0000.

That is the two's complement representation of -128. Notice how -128's representation is identical to +128's. This is why signed bytes can only hold up to +127. By convention it was decided to interpret 1000 0000 as -128 rather than +128. It was a sensible decision because it lets us think of the leftmost bit as a sign bit (1 for negative, 0 for non-negative).


Now you didn't say anything about 8 bits, so you're probably wondering why I said to drop the leading 0. You don't have to.

The thing about negative numbers is that they don't really have "a" sign bit. They have a lot of them. An infinite number of them. The computer can only hold so many bits, but conceptually speaking negative numbers have an infinite string of 1's to the left.

Let's see what I mean by doing the same thing as above but in a 16-bit context.

  • The 16-bit unsigned representation of +128 is 0000 0000 1000 0000.
  • Its one's complement is 1111 1111 0111 1111.
  • Adding one to it gives 1111 1111 1000 0000.

Notice how the 16-bit representation of -128 (1111 1111 1000 0000) is the same as the 8-bit representation (1000 0000) except it has a bunch of 1's on the left. This is known as sign extension. Negative numbers are sign extended with extra 1's if you make them wider. Non-negative numbers are extended with extra 0's.

      | 8-bit         | 16-bit
----------------------|--------------------
+128  | 1000 0000     | 0000 0000 1000 0000
-128  | 1000 1000     | 1111 1111 1000 0000

This means that calling the leftmost bit the "sign bit" is a bit of oversimplification. If it were a sign bit you could just flip it to change a positive number to negative. That's how signed magnitude works. It's not how two's complement works. When you negate a number in two's complement you often end up flipping a whole bunch of bits on the left.