1

During subtraction using 2's complement, why it is important to decide on the number of bits we are going to use, (e.g. 4 bit, 8 bit, 16 bit etc) and use the same number of bits to write each number. For example:

Question: (-2-5) base 10

      (-2)+(-5)

--> 2 in 2's complement: (1110) base 2, 5 in 2's complement: (1011)

--> (1110) + (1011)

--> 1001 with carry 1, we discard the carry and take 2's complement of 001 --> 111

--> Since MSB is 1, the answer is -(7) base 10, which is correct answer.

but if we do the same quation such that and writing only 3 bits.

2 in 2's complement is (110) base 2

5 in 2's complement is (011) base 2

if we add we will get (001) with carry 1, which we will discard Thus the answer will be (1) base10, which is incorrect answer.

Any specific reason we have to take 4, 8 or 16 bit. Why we cannot get answer with 3 bit, as both 5 and 2 can be represenetd in 3 bits.

TapasX
  • 405
  • 3
  • 17
  • 2
    *2's complement* is a confusing phase. It's both a noun (a notation for writing numbers) and a verb (an action you can take upon such notations.) 2 represented in 2's complement notation is 010. Not 110. But the 2's complement of 010 is 110. You need to keep your head on straight and know whether the noun or verb is intended. Also, you cannot represent -5 in 2's complement notation without at least 4 bits. 011 is NOT -5. It's 3. And 3 - 2 is 1. To get -5, you need at least 4 bits: 1011. (Or any sign-extended notation such as 11011, etc.) – jonk Dec 08 '21 at 06:26
  • 1
    The number of bits determine the amount of ones for negative numbers. 0xFFFF would be -1 with 16 bits and 0xFF would be -1 with 8 bits. Whereas 0x00FF with 16 bits is the positive number 255. – Lundin Dec 08 '21 at 08:54
  • Computer hardware has been historically built using fixed length arithmetic processing units. If a computation cannot fit in 8 bits, it has to use 16 bit hardware if present, otherwise the computation continues using the carry out mechanism: e.g. 8 bits (unsigned) will represent 0 to 0xFF or 255 decimal numbers) but for signed (2s complement) you can have 0_111_1111 or 127 decimal since MSB is reserved for the sign. On the other extreme, you can have 1_000_0000 that represents -128. Use [this](https://www.exploringbinary.com/twos-complement-converter/) converter to understand it further. – Syed Dec 08 '21 at 13:21
  • 1
    There is no such reason. You can use 3 bits, 5 bits, 17 bits whatever you want - as long as it encloses the range of numbers you need. (Which was not the case in your 3 bit example). –  Dec 08 '21 at 16:20

2 Answers2

2

-2 = 1110
-5 = 1011
____________
-7 = 1001

The answer is that -7 can't be represented in 3 bits. You need at least 4 bits to represent -7.

When doing twos complement addition your addends need to have at least as many bits as the result is expected to have. By pure chance, if you had not dropped the carry bit, you would have got the correct answer. But for the general case the addends should be sign extended to the correct number of bits before performing the operation.

From a purely mathematical point of view, picking 4, 8, 16 bits is nothing special compared to picking say 5 or 13 bits. But when designing real hardware there is sometimes efficiency to be gained by picking the number of bits to be a power of 2.

user4574
  • 11,816
  • 17
  • 30
  • Not such pure chance — if you're only adding two numbers than the result can only be one bit wider, which is why the "carry bit" concept works :) – hobbs Dec 08 '21 at 04:09
  • @hobbs I think you are right, about that as long as both addends are the same number of bits to begin with. – user4574 Dec 08 '21 at 04:16
  • Can it be more than 1bit greater than the longest word.... – Tony Stewart EE75 Dec 08 '21 at 04:40
  • @Tony No. Pretty much every ALU outputs the same size as the largest input size and uses one extra bit, which is called the Carry bit, stored in a status word along with some other interesting status bits (exact zero result, signed overflow, etc.) If you had a base smaller than 2, say 1.5, then 11 would require 6 and the sum of 11+11=22 would require 8. So I suppose it would be possible to need more than one greater than the longest word. But I don't often see those bases used. ;) – jonk Dec 08 '21 at 06:18
  • @jonk agreed. My question was intended to a Socratic type for user4574 but good comment, Carry on. I recently revisited irrational base addition in nature using base-φ – Tony Stewart EE75 Dec 08 '21 at 06:55
1

Why we cannot get answer with 3 bit, as both 5 and 2 can be represenetd in 3 bits.

You are correct that 5 and 2 can be represented in 3 bits. But -5 cannot, you need at least one more bit. Your value "011" is not -5, it is 3.

The same is true for -7, it is 1001 and needs 4 bits.

-2 can be represented in 3 bits, though, as you found: 110.

Any specific reason we have to take 4, 8 or 16 bit.

There is no specific reason, as I pointed out above, you need at least 4 bits to represent -5.

The reason for powers of two in bit widths is technical. Today engineers love to use such powers, perhaps because each bit can be "addressed" by a binary number. However, the history of computer saw multiple other systems with 6 bits, 9 bits, 12 bits, 14 bits, and even 36 bits.

the busybee
  • 2,140
  • 8
  • 20