How to convert positive number to negative number in Two's complement code?

Discussion in 'Homework Help' started by Osvaldon, Sep 24, 2016.

  1. Osvaldon

    Thread Starter New Member

    Sep 24, 2016
    5
    0
    I have two numbers in Two's complement code A and B, where both of them are positive.
    I need to subtract B from A ( A - B).
    How can I do this when I can do the following:
    Perform shifts, invert, +1, -1, SUM, XOR.

    I think I should convert B from a positive number to a negative and them sum A and B, but I got not idea how to do that.
     
  2. dl324

    Distinguished Member

    Mar 30, 2015
    3,235
    619
    Welcome to AAC!

    Is this homework?
     
  3. Osvaldon

    Thread Starter New Member

    Sep 24, 2016
    5
    0
    Hello, yes it is. I'm supposed to create a micro command for the CPU (or something like that) that would do the following A - B / C^2. I got division and multiplication covered, but I got no idea how to convert B to a negative number so I could A + B, so I'm basically stuck because of it.
     
  4. dl324

    Distinguished Member

    Mar 30, 2015
    3,235
    619
    What is the relationship between a number and it's two's complement?
     
  5. Osvaldon

    Thread Starter New Member

    Sep 24, 2016
    5
    0
    Do you mean how to transform a number into two's complement? I think it's inversion then +1 and you got a number in it's two's complement.

    What if I were to subtract 1 from B, then do a logical shift left and then right and finally add +1 again, would that work?
     
  6. dl324

    Distinguished Member

    Mar 30, 2015
    3,235
    619
    What is the relationship between a number and it's two's complement? For example 3 and it's two's complement.
    This is homework. You tell me.
     
  7. WBahn

    Moderator

    Mar 31, 2012
    17,716
    4,788
    MOD NOTE: Moved to Homework Help.
     
  8. WBahn

    Moderator

    Mar 31, 2012
    17,716
    4,788
    You are making a common mistake in your thinking. You do not do this to transform a number into two's complement -- you do this to find the representation of that number's complement (i.e., it's additive inverse) as represented in two's complement. This is conceptually a very different thing.

    If I have a value, A, and I have the representation of that value in two's complement, then inverting all the bits and adding one gets me the two's complement representation of -A; and it doesn't matter whether A is positive or negative.
     
  9. WBahn

    Moderator

    Mar 31, 2012
    17,716
    4,788
    But why do you need to do A + B at all?

    Your equation has you finding the square of C. Then dividing B by the square of C. Then subtracting the result of B divided by the square of C from A.

    Perhaps you need to review order of operations?
     
  10. WBahn

    Moderator

    Mar 31, 2012
    17,716
    4,788
    So what you are saying is that you think you should perform

    A + (-B)

    Sounds reasonable.

    So how do you take the negative of a two's complement number?
     
  11. Osvaldon

    Thread Starter New Member

    Sep 24, 2016
    5
    0
    My mistake. It's (A - B) / C^2 not A - B / C^2

    Invert B and add one?

    So if I take a binary number, invert it and add one, I get it's represantation in two's complement, then if I repeat the same action I get a represantation of that same, but negative number in two's complement?
     
  12. MrChips

    Moderator

    Oct 2, 2009
    12,421
    3,357
    No.
    Two's complement is a number representation, it is a system for representing positive and negative integer values.

    If you have a positive number, its two's complement will be the representation of the negative value.
    Forming the twos complement of a negative number will give you the positive value.
     
    Osvaldon likes this.
  13. WBahn

    Moderator

    Mar 31, 2012
    17,716
    4,788
    No. If you invert it and add one, you get the representation of the additive inverse of that number. If you invert it and add one again, you get the original representation back.

    If you have a positive number, then the binary representation of that number is the same either as an unsigned binary representation or as a two's complement representation.
     
    Osvaldon likes this.
  14. Osvaldon

    Thread Starter New Member

    Sep 24, 2016
    5
    0
    Ok thanks, that's what I needed.

    Free likes for everyone!
     
  15. dl324

    Distinguished Member

    Mar 30, 2015
    3,235
    619
    Post how you implemented A-B so we'll know you got it.
     
    Osvaldon likes this.
Loading...