How Does Unsigned Subtraction Work in Modern Computers Using Complements?

  • Thread starter Thread starter adjacent
  • Start date Start date
  • Tags Tags
    subtracting
Click For Summary
Modern computers perform subtraction using the method of complements rather than dedicated circuits. When dealing with unsigned numbers, such as 15u - 14u, the process involves interpreting the subtraction as addition of the two's complement of the second operand. Although -14 cannot be represented in a 4-bit signed range, its two's complement representation (0010) allows the operation to yield the correct result when added to 15 (1111). The discussion highlights that while the two's complement method works, it raises questions about the representation and interpretation of numbers outside the valid range for 4-bit signed integers. Ultimately, this process illustrates the flexibility of modular arithmetic in computing, particularly for unsigned operations.
adjacent
Gold Member
Messages
1,552
Reaction score
62
Modern computers don't have circuits dedicated for subtraction. They do it by using the method of complements.

All is well when the 2 operands are signed 2's complement numbers. The result of addition will be another 2's complement which, when interpreted as a 2's complement number, will give the correct result provided that there's no overflow.

Consider a 4 bit number. The unsigned range is 0 <-> 15. Signed range is -8 <-> 7.

What happens when I do 15u - 14u? The first thought which comes is "Do the 2's complement of 14 and do 15 + (-14)". But then, -14 is not in the range of a 4 bit number.

But if I do it anyway (Flip all bits and add 1):
Code:
  1111
+ 0010
------------
1  0001  =  0001 4 bits
It seems to work. My problem is that 0010 is not equal to -14 in 2's complement. It's just 2.

Question: Why does it work? And is this even the actual way it's happening?
 
Technology news on Phys.org
adjacent said:
It seems to work. My problem is that 0010 is not equal to -14 in 2's complement. It's just 2.
0010 is the 2's complement representation of -14, working with 4-bit numbers.
You can convince yourself of this by adding 1110 (the 4-bit representation of 14) and 0010 (the 4-bit 2's complement representation of -14). You should end up with 1 0000, with the lower 4 bits being all zero.
adjacent said:
Question: Why does it work? And is this even the actual way it's happening?
 
  • Like
Likes adjacent
Mark44 said:
0010 is the 2's complement representation of -14, working with 4-bit numbers.
You can convince yourself of this by adding 1110 (the 4-bit representation of 14) and 0010 (the 4-bit 2's complement representation of -14). You should end up with 1 0000, with the lower 4 bits being all zero.
If the range of 4 bit signed is -8 <-> 7, how come -14 fit in there?
Or would it be more wise to call it modular additive inverse instead which happens to be same as 2's complement for unsigned numbers? It actually makes more sense.
 
adjacent said:
If the range of 4 bit signed is -8 <-> 7, how come -14 fit in there?
You're right -- -14 is not in that interval. What I said was applicable for 5 or more bits, not 4.
adjacent said:
Or would it be more wise to call it modular additive inverse instead which happens to be same as 2's complement for unsigned numbers? It actually makes more sense.
 
Mark44 said:
What I said was applicable for 5 or more bits, not 4.
I'm not sure what you mean now. Could you elaborate?
What exactly is it for 4 bits? 2's complement or modular additive inverse?
 
For an unsigned 4-bit number, the range is 0 through 15. For a signed 4-bit number, the range, as you know, is -8 through 7, so neither 14 nor -14 is in that range.

With regard to a processor with 4-bit registers, I suspect that what happens when 15 - 14 is computed:
1. 15 - 14 is interpreted as 15 + (-14)
2. -14 is converted to its 2's complement form, or 0010
3. 15 - 14 is interpreted as 1111 + 0010, which results in 0001, which is the correct answer.
 
  • Like
Likes adjacent
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
1
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
3
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K