1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Arithmetic with signed binary

  1. Sep 27, 2011 #1
    Why is it that when using 2's complement, the result of arithmetic operations differ by two?

    11011001 (-39) +
    11100111 (-25) =
    11000000 (which is -62 in 2's complement, even though it's supposed to be -64)

    00110011 (51) +
    11101110 (-16) =
    00100001 (33, but it's supposed to be 35)
     
  2. jcsd
  3. Sep 27, 2011 #2

    lewando

    User Avatar
    Gold Member

    Some errors:

    -64 is 11000000 in 2's complement.

    -16 is 11110000 in 2's complement.
     
  4. Sep 27, 2011 #3
    Am I doing it wrong?
    11000000
    => 00111110
    = 2^1 + 2^2 + 2^3 + 2^4 + 2^5 = 62
    => -62

    11110000
    => 00001110
    = 2^1 + 2^2 + 2^3 = 14
    => -14
     
  5. Sep 27, 2011 #4

    eumyang

    User Avatar
    Homework Helper

    Looks like you're subtracting one after inverting the bits? I believe you always add one, so it should be
    00111111 + 1 = 01000000

    and
    00001111 + 1 = 00010000
     
  6. Sep 27, 2011 #5

    lewando

    User Avatar
    Gold Member

    Agree with eumyang.

    The algorithm for dealing with negative numbers (positives are easy--do nothing) is this :

    To convert, say -64 into 8-bit 2's complement:
    1) Take the 8-bit binary of the positive part (64): 0100 0000
    2) Since the original value is negative, complement the result from 1) and add 1: 1011 1111 + 1 = 1100 0000


    To find the decimal given the 8-bit 2's complement value, say for 1100 0000:
    1) Note that the MSB is a 1 and therefore your result will be a negative number.
    2) Complement the original value: 1100 0000 => 0011 1111
    3) Add 1: 0011 1111 + 1 = 0100 0000 (which is binary 64)
    4) Apply the negative sign from part 1) to the result from part 3) "-" : 64 = -64
     
  7. Sep 28, 2011 #6
    I see where I was wrong... I thought I didn't need to change the last bit in any way, so when inverting I always leave the last bit alone. Thanks!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Arithmetic with signed binary
Loading...