# Comparing and adding numbers using multiplexers and comparators

Supposing I have been given three two bit numbers, a1a0,b1b0,c1c0 then what is the best way to design a circuit that gets the largest two of the three and adds them?

• I can tell that you've attempted to solve this on your own. And as such I shall continue where you left off to give a good answer. – Harry Svensson Oct 22 '17 at 10:06
• @HarrySvensson I have not studied this for a decade. I am trying to see how to conceptualize logic level of thinking. I tried only for two two bit numbers without a comparator and this is trivial and this max and add problem (I thought) was interesting in its own right. At the programming level it is just couple of if statements and add. How much complexity does it take to get a gate level implementation is the curiosity that drove this problem. – T.... Oct 22 '17 at 10:25
• You can think of an "if" statement as an AND operation -- and any associated "else" as AND NOT. If both the "if" and the "else" assign to the same variable, then you have a multiplexer. – Dave Tweed Oct 22 '17 at 11:18
• Don't try to implement this as described, instead collapse it and just implement a function of 6 inputs which gives the correct output. Probably as thee distinct 6 input functions each giving one bit of the output. – Chris Stratton Oct 22 '17 at 16:43
• @ChrisStratton but comparator and mux way of thinking is more manageable for large scale projects. correct? – T.... Oct 22 '17 at 21:15

## 1 Answer

We can either look for the two largest, or we can look for the one smallest and sum the other two.

In my opinion, finding the smallest feels more intuitive than finding two largest.

Let's see in pseudo-code how that can be accomplished.

//find smallest, and sum the others
s0=0 //introduce another 2-bit variable called s
s1=0 //it essentially keeps the sign of the addition between the comparisons
if(a>b){
s0=1
}
if(b>c){
s1=1
}

//mini truth table
//s1 s0                             verdict                        sum
// 0  0     b<=c  a<=b       =>   a is smallest                    b+c
// 0  1     b<=c  a>b        =>   b is smallest                    a+c
// 1  0     b>c   a<=b       =>   either a or c is smallest        b+?
// 1  1     b>c   a>b        =>   c is smallest                    a+b

// looks like we need a third check for s1 = 1 and s0 = 0
s2=0
if(a>c){
s2=1
}

//s2 s1 s0                          verdict                        sum
// 0  1  0  b<=c  a<=b a<=c  =>  a is smallest                     b+c
// 1  1  0  b<=c  a<=b a>c   =>  c is smallest                     b+a


So here's my result on a white board.

• The / sign means the bit width of the wire.
• The red x means that I throw away the result.
• Some of the input to the adders got a -, that's 2's complement.

If this is too high level for your needs, then convert the mux into the AND gates that it's made of, and make your 2bit adder with the gates that it's made of.

## Edit

It wasn't 100% correct the first try, I messed up with s1=1 and s0=0. And I ignored s1=1 and s0=1. This will be bulletproof and definitely work. I've updated the pseudo-code above so it's correct.

Here's the correct block diagram.

• Let me digest this... and get back. – T.... Oct 22 '17 at 21:14
• This is why it's always great if the person making a question makes any kind of effort, that way it's much easier to continue where you left off. Rather than giving an answer that is indigestible. – Harry Svensson Oct 22 '17 at 21:57