Shop OBEX P1 Docs P2 Docs Learn Events
how to change number to 2s compliment — Parallax Forums

how to change number to 2s compliment

ohVaNiLLaGoRiLLaohVaNiLLaGoRiLLa Posts: 33
edited 2013-04-24 00:23 in Propeller 1
I want to take an input of an 8 bit number each bit on a different pin so 8 inputs. Then I want to take the 2s compliment of that number. Is there an easy way to do this? After I get the 2s compliment I am going to output the number on another 8 or 9 pins.

Comments

  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-04-23 18:40
    That's what the - operator does. Right?
    outa[OUTMSB..OUTLSB] := -inputValue
    
  • kwinnkwinn Posts: 8,697
    edited 2013-04-23 19:29
    What Jon posted if this is going to be done on a propeller in spin. In hardware you have to invert every bit and add one to the result.

    Are you going to use a prop for the TTL calculator project?

    BTW, not a good idea to post in multiple forums for a single topic.
  • ohVaNiLLaGoRiLLaohVaNiLLaGoRiLLa Posts: 33
    edited 2013-04-23 19:48
    no i am not going to use it for my calculator. i was just curious as to if there was a simple function that would do it or not if i were to use it as a part for my ttl calculator.

    and doesnt the - operator just make it negative? that would give the 1s compliment but not the 2s. Or am I mistaken?
  • kwinnkwinn Posts: 8,697
    edited 2013-04-23 21:02
    The - operator converts it to 2's complement. Makes adding and subtracting in binary simpler. See http://en.wikipedia.org/wiki/Two's_complement for an explanation.
  • AribaAriba Posts: 2,690
    edited 2013-04-23 23:56
    no i am not going to use it for my calculator. i was just curious as to if there was a simple function that would do it or not if i were to use it as a part for my ttl calculator.

    and doesnt the - operator just make it negative? that would give the 1s compliment but not the 2s. Or am I mistaken?

    1s complement is all bits inverted. 2nds is inverted an 1 added, which is the same as negate for signed numbers.

    Andy
  • Heater.Heater. Posts: 21,230
    edited 2013-04-24 00:23
    Here is what you can do:

    1) Adding any numbers, positive or negative, requires no special treatment. Just run them through your adder and you will get the correct signed results.

    2) For subtracting a number you need to negate it first. That is take the two's compliment, as has been noted above. That is invert all the bits and add one.

    3) The simple way to do that is:

    a) Invert all the bits on their way to the adder.

    b) Set a "carry in" bit into bit 0 of the adder.

    (I'm assuming you are not using an ALU chip with an add/subtract control line)

    How do you switch this arrangement from adding to subtracting? Use XOR gates between operators input bits and the adder. The second input of each XOR is driven by an add/sub select signal that will cause the XORs to invert the input or not. Also use the add/sub select signal as the carry in bit of the adder.

    You see the trick here? You don't build a special unit to do "twos compliment" of one of you operators. Instead you do it with the help of the adder itself. Invert all the bits with the XOR gates and get the +1 by feeding the carry bit in. Much simpler.

    This is much more interesting and educational way to solve the 8 bit calculator problem than knocking up some MCU code.
Sign In or Register to comment.