0
$\begingroup$

So I was reading the use complement operation in the digital logic and found, it is used to give additive inverse of the number and subtracting a number (like we do) is difficult for computer. So it adds the negative representation of the number, which is $a - b = a + (-b)$.

Now both complement's used to represent the negative of the number, but why 2's complement is preferred? This is my main question

#include <iostream>
#include <bitset>

int main() {
    std::cout << std::bitset<8>(7) << std::endl;     // 00000111
    std::cout << std::bitset<8>(-7) << std::endl;    // 11111001
    return 0;
}

Fyi, I know that in 1's complement there can be two zeros $+0$ or $-0$.

$\endgroup$

2 Answers 2

2
$\begingroup$

As you said, one's complement wastes one configuration of bits. Moreover, it requires additional logic to handle the two representations of zero. It also requires a more complex (hardware) implementation of addition when the operands have different signs (in two's complement the addition implementation is exactly the same as the one for "unsigned" numbers).

$\endgroup$
1
  • 2
    $\begingroup$ Because I wanted to post it as an answer: Yes, the biggest advantage of two’s complement is that add/subtract for signed and unsigned integers is exactly the same. And n bit product of signed and unsigned n bit integers is exactly the same, as is comparison. When flags are set you often have different flags for signed and unsigned integers. Like “if the numbers you just added are signed then you got an overflow. If they were unsigned you had a carry. These are not the same”. $\endgroup$ Commented Oct 24, 2023 at 13:32
2
$\begingroup$

The main reason is that there needs to be no special handling to perform arithmetic with negative numbers. The hardware does not need to check the sign bit and inverse the operation (add becomes subtract and subtract becomes add) if the sign bit is set as would be necessary with Sign & Magnitude.

So why does Two's Complement have this property? Let us use base 10 as an example. Suppose we have a base 10 number that only has a fixed number of digits. For example 4 digits. We could represent any number between 0 and 9999. But note that if we add any multiple of 10000, there is no effect since the only digits that change are overflowed and therefore ignored. As such, adding 10000-n is equivalent to just subtracting n. 9999 would effectively be -1. Because for example if we add 1234+9999 we get 11233 but then we forget about the leading 1 because we only have 4 digits so we end up with just 1233 which is in fact 1234-1. Similarly, adding 9998 would be like subtracting 2, and so on.

If we apply the same logic to binary instead of base 10 we can see why the Two's Complement representation works. If we have a 4-bit number and add 0b1111 that is the same as subtracting 1 because this is equivalent to adding 0b10000 then subtracting 1 then discarding the extra leading bit. And by the way 0b1111 is Two's Complement for -1 for 4 bits.

This same logic can be extended to any number of bits or any number that is 'negative' in Two's Complement.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.