Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Unsigned subtraction

  1. Aug 12, 2016 #1

    adjacent

    User Avatar
    Gold Member

    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 (Text):

      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?
     
  2. jcsd
  3. Aug 12, 2016 #2

    Mark44

    Staff: Mentor

    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.
     
  4. Aug 12, 2016 #3

    adjacent

    User Avatar
    Gold Member

    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.
     
  5. Aug 12, 2016 #4

    Mark44

    Staff: Mentor

    You're right -- -14 is not in that interval. What I said was applicable for 5 or more bits, not 4.
     
  6. Aug 13, 2016 #5

    adjacent

    User Avatar
    Gold Member

    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?
     
  7. Aug 13, 2016 #6

    Mark44

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Unsigned subtraction
Loading...