1
\$\begingroup\$

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.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ 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.) \$\endgroup\$
    – jonk
    Commented Dec 8, 2021 at 6:26
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Lundin
    Commented Dec 8, 2021 at 8:54
  • \$\begingroup\$ 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 converter to understand it further. \$\endgroup\$
    – Syed
    Commented Dec 8, 2021 at 13:21
  • 1
    \$\begingroup\$ 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). \$\endgroup\$
    – user16324
    Commented Dec 8, 2021 at 16:20

2 Answers 2

2
\$\begingroup\$

-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.

\$\endgroup\$
5
  • \$\begingroup\$ 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 :) \$\endgroup\$
    – hobbs
    Commented Dec 8, 2021 at 4:09
  • \$\begingroup\$ @hobbs I think you are right, about that as long as both addends are the same number of bits to begin with. \$\endgroup\$
    – user4574
    Commented Dec 8, 2021 at 4:16
  • \$\begingroup\$ Can it be more than 1bit greater than the longest word.... \$\endgroup\$
    – D.A.S.
    Commented Dec 8, 2021 at 4:40
  • \$\begingroup\$ @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. ;) \$\endgroup\$
    – jonk
    Commented Dec 8, 2021 at 6:18
  • \$\begingroup\$ @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-φ \$\endgroup\$
    – D.A.S.
    Commented Dec 8, 2021 at 6:55
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.