# absolute numbers subtractor

I have to subtract two binary numbers, each two bits long.

so the numbers are x0x1 and y0y1. the result z0z1 has to be the absolute value.

At my disposal are: a two bit subtractor (ie it subtracts two numbers of two bits each and puts out two bits which is the result)

I also have a magnitude comparator for two number each two bits.

The magnitude comparator has three outputs: x > y, x = y, x < y

I also have 4 xor gates.

I figured the best way to approach this problem is to let the subtractor do the subtraction and if y0y1 > x0x1 then swap the inputs to the subtractor.

I'm thinking that I have to somehow use the 4 xor gates and the magnitude comparator to do the switching if the case above is true. but I can't figure out how.

• so, you need to subtract two numbers, and you have a subtractor. I'd say you're done, or you haven't told us everything about that subtractor. Also, these are a whole lot of 16 possible x - y combinations, so frankly, write a table, use as LUT or find minterms, be done. This exercise is extremely awkward. Sep 2, 2017 at 20:12
• I assume your inputs are positive-valued, not 2's-complement. (For 2s-complement your adder would need 3 outputs.) The fact that you have 4 XORs leads me to think that you must apply them at the inputs rather than at the outputs. That means that the only source for the invert/not-invert is the comparator, with the inputs as input. Does that help? Sep 2, 2017 at 20:13
• I removed the "here is my question", because it's redundant, and I removed the "thank you" phrase, because it's also redundant. Using such phrases is frowned upon here, because it distracts from the question. Please don't add them back. Sep 2, 2017 at 20:14
• @marcus I thgink you missed the clause "the result z0z1 has to be the absolute value." Sep 2, 2017 at 20:15
• @MarcusMüller, I have a subtractor but I need the absolute value, basically if x > y then I need to do y - x instead of x -y. that way I get the absolute value. in order to do this switching I have 4 xor gates and a magnitude comparator. Sep 2, 2017 at 20:16

Here's the schematic I'd been discussing:

simulate this circuit – Schematic created using CircuitLab

It was just a matter of inverting the inputs, based upon the comparison output.

It's a little excessive to do it that way, but also pretty obvious when you think about it. (I gather those were the parts you are given to work with.)

A simplification isn't too difficult:

simulate this circuit

But it is a little bit less obvious, until you look at the algebra (or table.)

• yes, it's basically using the fact that A-B = (-A)-(-B). why didn't I think about that. Thank you very much! Sep 3, 2017 at 9:01
• @eventhorizon02 I'm sure you won't forget it, now! Best wishes.
– jonk
Sep 3, 2017 at 9:14

Since you only have 16 possible cases, it might be easier to do this with a lookup.

Even if not, write out the whole truth table. It's only 16 lines, with a 2-bit output for each line. Then think of this as a stricly combunatorial function. See if you can simplify things. Look at the function for each output bit separately. Maybe a few simplifications emerge. If not fall back to the lookup.

You also need to specify the format of the input values. Are they 2s complement and can be negative or not, for example.

• I have a subtractor that takes two numbers and and outputs the subtraction result, the numbers are two bit each. so no need for two's complement. I also have a magnitude comparator that compares two numbers. the comparator has 3 output legs: the legs are one for x>y one for x<y and one for x=y. I also have 4 xor gates. I need to somehow use the comparator and the 4 xor gates so that the subtractor always has in its x input the bigger number. Sep 2, 2017 at 21:46