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
SUMMARY

Modern computers perform subtraction using the method of complements, specifically 2's complement for signed numbers. In a 4-bit system, the unsigned range is 0 to 15, while the signed range is -8 to 7. When calculating 15u - 14u, the operation is interpreted as 15 + (-14), where -14 is represented as 0010 in 2's complement form. This method works correctly within the constraints of 4-bit arithmetic, despite -14 being outside the signed range.

PREREQUISITES
  • Understanding of 2's complement representation
  • Knowledge of binary arithmetic
  • Familiarity with signed and unsigned number ranges
  • Basic concepts of modular arithmetic
NEXT STEPS
  • Explore the mechanics of 2's complement in different bit-widths
  • Study modular arithmetic and its applications in computer science
  • Learn about overflow behavior in binary arithmetic
  • Investigate how different processors handle arithmetic operations
USEFUL FOR

Computer scientists, software engineers, and anyone interested in low-level programming and binary arithmetic 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   Reactions: 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   Reactions: adjacent

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
13K
  • · 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