Electronic – Which 4-bit Binary Number Is Greater

adderbinarycomparison

My Task

I'm working on an extra work question our teacher assigned us from the book. Design a combinatorial circuit that compares two 4-bit unsigned numbers A and B to see whether B is greater than A. The circuit has one output X, so that X = 1 if A < B and X = 0 if A = B or A > B.

My Ideas

I had 2 ideas, but neither are very good. 1 was to get 2's complement version of A(positive) and B(negative). I could then add them together and if we had a negative, it would be obvious that B is greater than A. The leading bit would be 1 so I would just negate the leading bit and have that be my output. Else it would output 1. This is almost a solution, but I don't think it would work if we had A = B because then the leading bit would be 0 but we would have an output of 1 which would indicate A>B when in fact A = B.

My second idea was to split each number into 4 bits, and compare the bits of each number and find out that way. This would require a 256 row truth table though, and I'd rather just not answer the question than write that out.

Neither of my approaches really seem feasible(I don't even feel as though they accomplish the task). Can anyone suggest a hint as to what would be a good approach or idea to accomplish this task? (Not looking for a full out solution, just something to get me started)

Best Answer

If you want to find the largest number between two unsigned numbers A and B, all you need to look at is the most significant bit where the bits in A and B are not the same, the larger number is the number that will have a '1' at that point and the smaller number will be the number that will have a '0' at that point. e.g if

$$ \text{A} = 1100\\ \text{B} = 1011 $$

then A is bigger than B because at the most significant bit where the bit number's aren't the same (bit index 2 in this case) A is 1 and B is 0.

So to solve this problem, we can start by defining a variable X\$_i\$, where X\$_i\$ is high if i) the bits of A and B in the current bit index are not the same, ii) all bits of A and B have been the same in all indexes greater that the current index and iii) the bit of value of B in the current index, B\$_i\$, is high.Using this logic we can extract the values of X\$_i\$ = {X\$_3\$ X\$_2\$ X\$_1\$ X\$_0\$} as being

$$ X_3 = B_3(A_3 \mathbin{\oplus} B_3) \\ X_2 = B_2(A_2 \mathbin{\oplus} B_2) .\overline{(A_3 \mathbin{\oplus} B_3)} \\ X_1 = B_1(A_1 \mathbin{\oplus} B_1) .\overline{(A_2 \mathbin{\oplus} B_2)} . \overline{(A_3 \mathbin{\oplus} B_3)} \\ X_0 = B_0(A_0 \mathbin{\oplus} B_0) . \overline{(A_1 \mathbin{\oplus} B_1)}. \overline{(A_2 \mathbin{\oplus} B_2)} . \overline{(A_3 \mathbin{\oplus} B_3)} $$

we can then get our final result X as

$$ X = X_3 + X_2 + X_1 + X_0 $$