How Does Unsigned Subtraction Work in Modern Computers Using Complements?

  • Thread starter Thread starter adjacent
  • Start date Start date
  • Tags Tags
    subtracting
AI Thread 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
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top