Comparing is very useful and important to that too. Just think about for-loops, if-else, Quicksort, and much more.
It is placed in the ALU, beside the adders, registers and some logical operators.
For example, look at this easy C/C++ code:
One single line hides a very complicated sequence of commands from the compiler for the processor hardware.
So it’s a bit more work when you code in Assembler:
jne - jump if not equal
jng - jump if not greater
A machine code or machine language are only instructions stored somewhere.
This could look like this:
0x00B813A4 55 8B EC 81 EC C0 00 00 00
0x00B813B1 C4 04 3B AB 8B F4 68 3C 57
0x00B813CC E8 80 FD FF FF 83 CA 08 5F
0x00B813E3 CC CC 51 50 00 5B 81 CA CC
0x00B813F8 00 B4 56 58 00 FF FF FF 30
0x00B8140F 52 8B E1 00 00 D1 B1 00 00
0x00B81424 CC CC CC 1B D8 FF D8 3B 20
0x00B8143B A0 00 01 20 F1 F4 D7 08 67
Our brain sees/knows the larger number within ms. (-3 and 5)
(I don’t know what happens in our brains, but it’s very good in how it does it.)
OK. Now back to the digital part again.
One way to determine the larger number is when you go from MSB to LSB and XOR the two bits.
When you get “High” as output, you look which number has a “1” on this location. Voila, the bigger number.
But for this, a mechanism is needed which goes through all locations. Like a register (Needs much clock signals) or a loop-thing (Code gets bigger).
An other way to check which number is bigger, is the subtraction:
If you only want to know when two numbers are equal, an simple “equality comparator” can be build.
This design in NAND/ NOR/ NOT only:
For a more precisely output, an other circuit is needed. Every bits get compared to each other + an input from the previous bit.
This is a 4 bit comparator. It can be stacked and is built up with “blocks”. That’s why I decided to use this circuit.
My 16 bit build: (19cm x 18cm x 5cm)
The different sections can be easily recognized.
Because of the one bit output, it can only show two states.
If it should indicate when the numbers are equal, the board would need at least a 2 bit output.
So, in my case it shows the “greater than or equal” for a number. The other number gets “less than”.
The option for “equal” will be also needed for my computer project.
For this reason, I will split the “greater than or equal” with an “equality comparator” to two outputs.
Then I have 3 output pins for all states. I think that’s foolproof. Exactly mine.
Testing the board: