How do computers compare numbers?

493 views

Example: How does a computer know that 8 ist greater than 3?
Because it works in binary, how does it know that 1000 (8) is greater than 0011 (3)?

In: 2

8 Answers

Anonymous 0 Comments

Take a circuit with inputs A and B (your bits to be compared) and three outputs, one each for A>B, B>A, and A=B.

The electronic logic gates we will be using:

* NOT: Flips a bit, 1 to 0 or 0 to 1
* AND: Produces 1 if all input bits are 1, otherwise 0
* NAND: The opposite, produces 0 if all input bits are 1, otherwise 0
* NOR: Produces 1 only if all input bits are 0.
* XNOR: Produces 1 if all inputs are 0 or all inputs are 1, otherwise 0 (tests for equality).

Send B through a NOT gate then to an AND gate, and send A directly to the AND gate. A 1 off the AND gate says A>B. Reverse this, send A through another NOT gate then to another AND gate, and B directly to that AND gate, so a 1 off that AND gate says B>A. Also send both directly through an XNOR gate, and a 1 means they’re equal.

It’s a lot more complicated with a lot more wires crossing around if you compare multiple bits (your 1000 and 0011), and you use AND gates, NAND gates, and NOR gates. Your four-bit comparer will have about 30 gates compared to the one-bit’s five gates above.

You are viewing 1 out of 8 answers, click here to view all answers.