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!

2's complement

  1. Nov 29, 2012 #1
    1. The problem statement, all variables and given/known data
    Execute the arithmetic operations using 6-bit binary numbers in 2's complement representation.

    18+11

    (just going to list this first task)

    2. Relevant equations
    To convert from regular binary number, from LSB, do not invert initial o's, or first 1, but invert all other values.

    +18 = 010010
    -18 = 101110

    +11 = 001011
    -11 = 110101

    I understand the thinking of the MSB being, for -18 2's comp: -32, then adding 8, 4 and 2 = -18

    3. The attempt at a solution

    But I don't know when to use complements.

    A)18+11. I get the right answer using two methods:

    If I simply add their regular binary values I get 29.

    But I also get it right if I add both of their 2's complements, and then complement the answer.

    101110
    + 110101
    =[STRIKE]1[/STRIKE]100011
    answer complements into: 011101 = 29

    But now I had overflow, and it still gets it right.

    Which of these methods are correct, in accordance with logic, and even more important, in accordance with what was asked in the task?

    Seems to me, that using complements in this simply addition of positive numbers, first of all gave me overflow, but also required an additional complementing?

    B: When do I have to complement the answers? Is there a logic so I know when to do it?
     
  2. jcsd
  3. Nov 29, 2012 #2

    rcgldr

    User Avatar
    Homework Helper

    The normal and somewhat easier rule to complement a number is to invert all the bits (ones complement), then increment the result by 1.

    +18 = 010010
    -18 = 101101 + 1 = 101110

    -18 = 101110
    +18 = 010001 + 1 = 010010

    +11 = 001011
    -11 = 110100 + 1 = 110101

    -11 = 110101
    +11 = 001010 + 1 = 001011

    You got a carry, which doesn't matter for signed number addition. The carry is used for unsigned numbers. Overflow occurs when the sign of the result is wrong (I assume you're supposed to figure out how overflow is detected).

    You only complement the answers to check your work. Normally you never complement the answers.
     
  4. Nov 29, 2012 #3
    Yeah, I found a lecture video on youtube that explained that method for complementing, and it is good, I just learned MY way from my lecturer, and they both lead to the same result. Thanks for explaining it though!

    Good point about the -18 + -11.. I didn't write that, you did, but I did do the operation, when I used both their 2's complements.

    yeah, I am expected to understand how overflow is detected by then, this is an exam from 2008 haha, so that would be rather on the late side of things for me to understand whilst solving the exam :D

    Alright, tell me when I am wrong in this statement:

    Even if I am asked to solve various arithmetic using 2's complement, I will use the REGULAR binary value for the positive numbers, such as +18, and only use 2's complement for the NEGATIVE ones, such as -18. I would still comply with the demand of the task, that I use 2's complement method.

    18
    + (-11)

    I would use

    18's regular binary
    + 11's 2's complement

    Is this correct?

    PS:

    This is gold, exactly the kind of nuggets I am after.
     
    Last edited: Nov 29, 2012
  5. Nov 29, 2012 #4

    rcgldr

    User Avatar
    Homework Helper

    Overflow occurs when the sign of the result is wrong. Depending on the operation, addition, subtraction, multiplication, or division, the test for this would be different. Perhaps start with an easy one, such as how you would detect overflow in the case of addition.

    So far, you've only explained how to complement a number and how to add two numbers. What you were probably supposed to do is figure out how to add, subtract, multiply, and divide using two's complement.
     
  6. Nov 29, 2012 #5
    You'd think so, but we only cover doing addition using positive numbers, negative numbers, or both.

    Going to re-read this tomorrow, it's almost 2 AM here.

    Thanks a lot, rcgldr!
     
  7. Dec 1, 2012 #6
    Thanks again for pointing out that if I had done stuff correctly, I would never have to complement the answer.

    Add that to me realizing that the MSB can be seen as an actual a negative value and not just a symbol (making it easy to figure out the magnitude of negative numbers, such as 1001 = -8+1=-7), and understanding how the operator and number symbols interact, really makes cleared up my confusion.

    Thanks a lot ;)
     
  8. Dec 1, 2012 #7

    rcgldr

    User Avatar
    Homework Helper

    Note the addition of unsigned and two complement numbers is the same process, and that is how it's implemented on computers. Overflow is always generated based on the assumption that the numbers are signed. Carry bit is always generated based on the assumption that the numbers are unsigned. It's up to a program to choose which of these to use.

    You mentioned that part of this was on an exam back in 2008, so I'm a bit confused on what you're working on now.
     
  9. Dec 1, 2012 #8
    In order to help us prepare for the exam (in 7 days), we have been issued a bunch of old exams and their solutions.

    So I am going through them one by one, it's excellent training.

    It's how it's done here.

    I just did a bunch of calculations similar to the one we discussed above, and others with negative operators, and negative values, or both, and I got everything right. So I definitely learned something!
     
  10. Dec 1, 2012 #9

    rcgldr

    User Avatar
    Homework Helper

    In the case of addition, the overflow indicator bit is set if the sign bits of the two numbers to be added are the same and the sign bit of the result is not the same. This can be expressed as a boolean equation, but I don't think you'll need to know that.

    I'm not sure how much of the hardware stuff you're supposed to know for the exam. As mentioned, to generate the two's complement of a number, you can invert all the bits, then increment a number. If a hardware subtract operation is going to do a complement and add, it can avoid the increment step by using a "full" adder (one with a carry bit input in addtion to the two bit inputs) on the least significant bits, then just inverting the number to be subtracted, and getting the increment done as part of the add operations by always setting the "carry bit" input on the least significant bit's "full adder".
     
  11. Dec 1, 2012 #10
    Right.. I see adders are in the curriculum, but we had no papers on it and I don't see it in any of the exam sets I have.

    Currently just putting the finishing touches on things I know pretty well, improving it to knowing it perfectly, then I will go onto adders later, and read your post again. Right now it's Greek to me, because I haven't even looked at it yet. Surprised to even see it in the list to be honest.
     
  12. Dec 1, 2012 #11

    rcgldr

    User Avatar
    Homework Helper

    You probably don't have to worry about the hardware aspects such as an adder.

    The part about overflow is more about the logic of how addition is done for signed two's complement numbers, so it might help to understand this. For signed two's complement numbers addition, you can only get overflow if both numbers are positive and the sum is negative or if both numbers are negative and the sum is positive. The actual process of addition treats the numbers as unsigned, so the only difference is setting the overflow indicator.
     
  13. Dec 1, 2012 #12
    When you say hardware aspect, are you talking about a logic diagram? Showing the inputs going through logic gates and exiting as outputs? If so, we do a lot of that. I'm doing OK in that department.

    Or are you talking about actual implementation with electrical components. That we are not doing.
     
  14. Dec 1, 2012 #13

    rcgldr

    User Avatar
    Homework Helper

    By hardware I must mean the logic diagrams, and since you're already doing that, it shouldn't be an issue.
     
  15. Dec 11, 2012 #14
    Don't want to brag, but I aced this exam like a boss. Thanks again!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: 2's complement
  1. 2's complement (Replies: 2)

  2. 9's complement (Replies: 2)

  3. 2's Complement (Replies: 4)

Loading...