how to change number to 2s compliment
ohVaNiLLaGoRiLLa
Posts: 33
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
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.
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
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.