1
$\begingroup$

I've found and implemented a full adder to be able to do unsigned or two's complement addition on wikipedia: https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder

However, in my particular case, I'd like to use a sign bit instead of twos complement, due to there being a variable (unbounded) number of bits involved.

How can i do addition and subtraction with binary numbers using a sign bit? (say, it's a separate bit, not one of the binary digits per se).

I also only have access to AND and XOR.

Edit: Potatoswatter suggested that twos complement is still able to be done with an unbounded number of bits. What do you do when you want to add 6 bits and 8 bits together? Also, is multiply, divide and modulo easily done in twos complement? Keep in mind i have only AND and XOR, so don't have the ability to do if statements, the logic needs to be "branchless".

Thanks!!

$\endgroup$
  • 1
    $\begingroup$ You can have two's-complement with an unbounded number of bits. Just let the most-significant bit be the sign. Alternately, say that the sign bit "isn't one of the digits," let the digits be a number less than $2^N$ where $N$ is given in a length prefix or whatever, and let the sign bit be something else which subtracts $2^{N+1}$ from that number. Two's-complement almost always saves headaches. (Floating point is the big exception.) $\endgroup$ – Potatoswatter Oct 4 '15 at 8:31
3
$\begingroup$

This is called "sign-magnitide" or "signed-magnitude" representation. A Google search for "sign-magnitude adder" gives lots of useful results. I know this doesn't answer your question directly (I don't have enough reputation to simply comment your post), but you will find the answer there.

As for only having AND and XOR, this is the classic result that any logic gate can be constructed from AND and NOT. Note that XOR(1,a) = NOT(a). Now you have NAND and NXOR. NOR(a,b) = AND(NOT(a),NOT(b)). Now you have OR by negating NOR. All other gates can be constructed by negating inputs of these gates.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.