How do computers compare numbers?

484 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

Simple short answer – the ‘knowledge’ is programmed into them.

You can think of a computer(processor) as a physical object; eg a simple river that forks in two branches at any fork point. 1 branch is yes (1) the other one is no (0).Then you drop a duck and watch it go down.You set your rules before hand – the duck has this behaviour and if the duck is going down the yes branch, do this. Otherwise, do that.

We simply program a few basic rules in the computer and then play with those rules to create any behaviour we can think of.

Anonymous 0 Comments

It’s all in how computers represent negative numbers. Without getting into the nitty, gritty details, the way computers represent positive and negative numbers is such that all positive numbers (and 0) will have the first bit as a 0 and all negative numbers will have the first bit as a 1.

Another consequence of how computers represent negative numbers is that subtraction can be implemented via addition; you simply convert the second number into its negative. That is, it takes something like 8 – 3 and changes it into 8 + -3.

We can use both of these facts to compare two numbers, A and B.

We subtract one from the other: A – B.

The computer converts this into addition: A + -B.

Then examines the result, C. It checks the first bit of C to see if C is positive (or 0), or negative. If C is positive (or 0) it then checks to see if it is actually positive or 0 (all bits are 0).

So, if C is positive, then A was greater than B.

If C is negative, then A was less than B.

If C was 0, then A was equal to B.

Anonymous 0 Comments

It doesn’t know: it checks when it needs to. You could do it by checking bits from left to right and returning once the bits differ. For your example it would see that the leftmost bit of 8 is set but the leftmost bit of 3 isn’t, and so 8 is bigger than 3.

However the faster approach is simply to subtract one from the other and checking if the answer is above or below zero (or zero exactly). The subtraction command will set flags in the CPU depending if the result is zero, and if the result is above or below zero. All you need to do is call that command (which will subtract the two numbers and flip the right flags), and then check what flags got set. If the zero flag got set the numbers are equal. If the zero flag did not get set but the sign flag did the numbers are not equal and the latter one is larger than the first one. If neither flag is set the first flag is larger.

Anonymous 0 Comments

The computer doesn’t “know”. There is a mechanical procedure to do it. It’s the same one you learned in school.

If you want to test if A > B:

Scan the bits from left to right (most significant bit to least-significant)

– If the A bit is 1 and the B bit is 0 then stop. A > B

– If the A bit is 0 and the B bit is 1 then stop. B > A

– If the A bit is equal to the B bit, then go to the next bit

If all the bits were scanned, then A = B.

This is encoded in a circuit that has inputs for the two numbers.

Anonymous 0 Comments

Effectively, if you’re checking of A>B, the computer will do A-B, and throw away the result. What it cares about are the *registers* after the operation.t

Registers are essentially flags that get set after every instruction, and they can be used by subsequent instructions. For example, one of those registers will be the Zero register. That is set if the final result is exactly 0. Another one will be the Sign register, which is set if the result is negative.

Those registers are all done via the actual circuitry in the CPU using logical AND, OR, XOR, and NOT gates. They’re effectively created as a side effect of the calculation the CPU is doing.

Anonymous 0 Comments

On the CPU, there is a part called a binary comparator.
There are a lot of ways to implement one. But let’s just talk about that 8>3 example in an inefficient but easy way.
You can compare bit by bit. Ignore everything else, 1>0 and 0<1. The most significant bits are more important, so the fact that the 1s and2s place are bigger in 3 doesn’t matter, because the8’s place is 1 in 8 and 0 in 3.
It’s all just a matter of building the circuits so it follows a truth table.
If you want to see the actual lay out, just look up digital comparator.

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.

Anonymous 0 Comments

The function for this is built into the computer.

You can build something like this yourself using XNOR logic gates to check whether two given bit inputs have one being 1 and the other 0 or both being 1s or 0s. Once you can do that for a single bit you can chain it together for a number consisting out of more than 1 bits.

You can build a magnitude cooperator out of logic gates that have two bytes as inputs and three bits for greater lesser and equal as outputs.

In modern computers that is part of the many different built in functions in a chip that can be called via machine code at the lowest level.