r/AskComputerScience • u/OC-alert • May 25 '26
In a computer, why is 2 more than 1?
Or how does a computer increase a value? How does it "know" which value is higher?
Is there a place I can go to learn this?
I know pretty much nothing about computers and I want to learn about how signals form information.
47
Upvotes
36
u/MasterGeekMX BSCS May 25 '26
Doing a masters in Computer Architecture.
The computer does not "know" stuff per se. We instead make circuits that respond to given combinations of wires being on and off.
Then, we use a thing mathematicians invented, which is representing numbers in systems that aren't based in our 0 to 9 digits. As wires on a circuit can only be on or off, we choose the binary system, where we only have the numbers 0 and 1 to work. In binary, each position isn't worth ten times more, but two times more, so in binary 1 means one, 10 means two, 100 means four, 1000 means eight, and so on.
With that system, a series of wires that are like this: 🪫🔋🔋🪫🔋🪫 means 011010, which is a 26 in binary.
All fine and good. But how we do math? With circuits that respond to combinations of wires being on and off. There are three basic circuits:
Combining them, you can make circuits that can do sums, by sensing if each of the wires that make a number add up or not.
While I could go on and on with this, a video is worth a thousand words.
Core Dumped, a guy from South America, has an excellent series about this while topic, explained with nice animations: https://www.youtube.com/playlist?list=PL9vTTBa7QaQOoMfpP3ztvgyQkPWDPfJez
Ben Eater, an electronic engineer, has a series of videos where he builds a simple computer out of basic chips, explaining all the details: https://www.youtube.com/playlist?list=PLowKtXNTBypGqImE405J2565dvjafglHU
72
u/high_throughput May 25 '26
Sounds like you'd be interested in nand2tetris
11
u/Traveling-Techie May 25 '26
The field you are curious about is digital logic. Learn about logic gates: AND, OR, NOT, XOR, XAND. Learn how to wire them together to make a one bit memory (flip flop), and how to build adders and comparators.
8
u/unknownmat May 25 '26
I haven't seen this recommended yet, but I always recommend Petzold's Code to my non-Computer-Science friends and colleagues who are curious about how computers work. This short and approachable book will give you a pretty good answer to basic questions of how computers are able to embody and interact with concepts such as numbers.
1
u/OC-alert May 27 '26
Thanks guys, but I'm gonna be honest. I think I need to learn about how a computer actually electronically works to understand this in a meaningful way.
1
u/Competitive_Mud5528 May 26 '26
You shoud try https://nandgame.com/ It can be a little bit tedious but really eye opening. Even for me that studied cs in college it enable a whole understanding of how operators are built (in a really simple way)
3
u/wjrasmussen May 25 '26
0000 vs 0001 vs 0010 vs 0011 vs ... vs 1111
3
u/rexyuan May 25 '26
Look up boolean comparator circuits
2
u/andful May 25 '26 edited May 25 '26
A computer can perform subtraction and "knows" when a number is negative, positive or zero.
In computers, a way to knows if x > y, can be done by performing x - y > 0, i.e. by checking if (x-y) is positive.
I am oversimplifing, but what I am trying to convey is that comparison is not fundamentally a more "magical" operation then subtraction.
1
u/cormack_gv May 25 '26
I'll assume that you know how it represents 0 and 1, and that 1 is greater than 0. One such value is a bit. If you have a sequence of bits, you can represent base-2 numbers. Let's say you have three bits. There are 8 possibilities:
000 represents 0
001 represents 1
010 represents 2
011 represents 3
100 represents 4
101 represents 5
110 represents 6
111 represents 7
More generally, if you have K bits you and represent the numbers from 0 to 2^k in binary.
Or, if you prefer, the numbers from -2*(k-1) to 2^(k-1)-1 in binary two's complement. To show the two's complement interpretation of the eight numbers above, subtract 4 from each.
1
u/True_World708 May 25 '26
The way a computer “knows” that a 1 is greater than a 2 is by using a comparison circuit. It works pretty much the same way we would compare two numbers in real life. We start by taking a look at each number and comparing their orders of magnitude. For example, 197 is greater than 86 because 197 has a 1 in the hundred’s place and 86 only has a 0. If two numbers have the same order of magnitude and the numbers in the respective magnitude place are also the same, then we would move onto the next lesser order of magnitude and perform the same comparison. We would then keep repeating this process until we reach the one’s place and say the numbers are equal if there is no difference between them. This process is contained inside what’s called a comparator circuit one example of which is an IC 7485. These circuits can be implemented using Boolean logic gates as shown in the image linked below. https://www.electronics-tutorials.ws/wp-content/uploads/2013/08/comb21.gif?fit=520%2C161
1
u/finedesignvideos May 25 '26
A computer does not know which value is higher.
We humans have come up with numbers and noticed patterns in them and have a concept of higher and of increasing value. We also found the pattern to figure out which number is higher and how to increase a value.
We told computers that when I say "higher" I mean you should follow so-and-so steps (to check the pattern we humans recognized), and when I say "increase value" you should do so-and-so steps (again, according to the pattern we humans recognized).
So that is why 2 is more than 1. We told the computer how to write 2, how to write 1, and what steps to do when we ask it "which is higher?". And it does those steps and tells us 2 is greater than 1, while having no idea what higher even means or even that 1 and 2 are used for counting.
1
u/dnult May 26 '26
One way a processor knows if a number is less than equal to or greater than is to subtract one from the other and see if the result is 0 (equal) of if a borrow occurred.
1
u/ga2500ev May 26 '26
The computer doesn't "know" that it's higher. It computes it using a simple function: subtraction.
If you want to know the greater of A and B, subtract B from A and check the result:
If the result is positive, then A is bigger.
If the result is negative, then B is bigger.
If the result is 0 then they are equal.
Most CPUs set negative and zero flags on a subtract. Most also have conditional GOTO or JMP instructions based on a flag.
Hope that helps.
ga2500ev
1
u/AlwaysHopelesslyLost May 26 '26
There is an online course called And To Tetris. It is a two party course. Part 1 has you build a computer from nothing in an emulator. Bit by bit. This teaches you exactly how computers work and how they do math
Part two is writing software for that computer.
Part 1 does not require any prior experience.
1
u/green_meklar May 26 '26
Um...because it actually is, and we designed the computer to represent that?
If you're asking why the comparison 2>1 evaluates to true, that goes something like, the computer subtracts the 1 from the 2 and checks that the answer (which is 1) is greater than 0 by checking whether it has any nonzero bits.
There's an adder circuit that is literally designed to take in the bits for two numbers and spit out the sum of the numbers. We designed the circuit to do that specific operation by combining all the bits in the right way.
1
u/Worldliness_True May 26 '26
OP, I can recommend nandgame.com to learn exactly how this works
1
u/shangjiaxuan May 26 '26
Probably with some combinatorial logic. What I can currently think of for unsigned numbers is a 2n divide and conquer approach. (Compare each bit, compact that resut using more significant bit result two by two recursively until you run out of bits)
1
u/flug32 May 26 '26 edited May 26 '26
Here is what the Guide to x68 Assembly says:
This basically defines what >, =, and < mean.
As you can see, these are defined by the operation of subtraction. And here is how that works:
So we are not quite down to bare metal yet - this is explaining what the various assembly instructions do, not how they work per se. But it starts to give you an idea.
Here is an idea of how subtraction could work, using the more elementary operations NOT and ADD:
So NOT works by going through the value in binary and flipping each 1 to 0 and each 0 to 1.
How does ADD work? In brief, it XORs each pair of bits to get the add value, and then ANDs the two bits to get the CARRY (which is then carried over and added to the next most significant bits).
In more detail:
So in its essence ADD is not too hard and simply relies on the more fundamental XOR and AND operations. But in actual implementation on a modern CPU all this gets pretty hairy. Here is an explanation of that in more detail.
But just from the above, you can see how every operation we depend on day to day - from comparing two values to see which is larger, to adding and subtracting values, can be calculating from more primitive operations like AND, OR, XOR, the ability to store and retrieve various values in binary form, and the ability to loop and do repetitive operations on various pairs of bits.
1
u/rb-j May 26 '26
There's a lotic circuit called a "full adder". It adds two bits along with a carry in (so that's three bits) and results in two bits, one representing the LSB of the sum and the other is the MSB of the sum which becomes the carry-out.
These are chained together to as many bits as you word width. This adds two binary numbers.
You can turn an adder into a subtractor by flipping the bits of the number to be subtracted and putting a 1 into the carry-in of the LSB full adder.
We compare numbers by subtracting one from the other and see if the result is positive, zero, or negative.
1
u/immortal_lurker May 27 '26
This is a deep question, and the first person to understand it (that we know of), was Ada Lovelace. Frequently called the first programmer, though it seems likely others were earlier.
Basicall, it is purely by implementation. All a computer is doing is setting voltages on and off. It does so every clock cycle, based on inputs, its own hardware level rules, and the voltages already present in the computer. Your monitor is set up to turn voltages into pixel brightness, so if the voltages are set correctly, you see things on your screen.
Your computer only knows that 2 is more than 1 because the people who built it know the rules for numbers, and implemented them correctly. Asterisk. They implemented them mostly correctly. Its really only good with intergers. And intergers that aren't too big.
If you're asking, "wait, what did you mean by that? What rules, exactly, did they have to implement?", then you have a long journey ahead of you. Start with logic gates. Work your way up to a binary adder. Then look up Von Nueman architecture. These are difficult hills to climb, but rewarding.
1
u/Both-Expression4402 May 27 '26
It performs subtraction. If the answer is positive, then it is higher, if the answer is 0, it is equal, if the answer is negative, then it is lower. Positive and negative numbers (integers) tend to be represented by twos-complement integers, which the highest digit (all the way to the left) determines if a number is positive or negative. For floating point (decimal) numbers, the process is often more complicated, involving small dedicated circuits in the ALU that can break down floating-point comparison into several dedicated sub-cases.
1
u/Quiet_Win8624 May 27 '26
id recommend you read the book "Code : hidden language of computer hardware and software" by charles petzold you said you pretty much know nothing about the computers trust me just pick up that book go read it you will get most of the answers you are looking for
1
u/aagee May 27 '26
Because we designed a circuit that sets the "greater than" flag when the two numbers are given to it.
1
u/IlgantElal May 27 '26
So, computer engineering is a lot of this. Basically in terms of closeness to funduntals: Electrical Engineering -> Computer Engineering -> Software Engineering -> Cyber Security and Machine Learning Engineering.
So, if you know the basics of electricity, then your next step is Boolean and Binary logic
1
u/filip1299 May 28 '26
Great book: "code the hidden language of computer hardware and software" it goes from flashlight to transistors.
1
u/JGhostThing May 28 '26
OK. Think about this.
Integers are stored in memory locations on the computer. These numbers are stored in a declared format. In modern computers this is often twos-complement. There are other formats.
There are modules in the computer which do comparison.
If you really have to learn computers at this level, you should study assembly language, after you have fully studied a real language.
1
u/Grobi90 May 28 '26
Watch Ben Eater build a breadboard computer.here
or watch Sebastian Lague simulate one in a little game thing that you can also work through here
As a lay-person, these two really do great explanation.
1
u/VArt21 May 31 '26
Here I present essential explanation how digital computer performs summation. Note: reddit editor does not allow for me to write subscripts for variables, (or allow but I don't know?) therefore for general formula - see below - for variables c, x, y, z adjacent n+1, n, i, 1 accept as subscripted indexes.
Example: suppose computer cells, each of them comprises four binary compartments. Suppose computer to add 5 = x and 7 = y; 12 = z is result. In binary form:
0101 x
+0111 y
1100 z
For addition, semiconductor schemes fulfils these logical operations: v (or), & (and), 🜨 (xor (exclusive or)). Let’s introduce auxiliary variable c (carry). Because of four cell compartments, computer does four main additions, each of them has two steps, of which first results in digit for z, while second is carry.
1) 0 🜨 1 🜨 1 = 0 //first 0 is carry; result 0 is 1st digit of z
0 & 1 v 0 & 1 v 1 & 1 = 1 // got carry 1
2) 1 🜨 0 🜨 1 = 0
1 & 0 v 1 & 1 v 0 & 1 = 1
3) 1 🜨 1 🜨 1 = 1
1 & 1 v 1 & 1 v 1 & 1 = 1
4) 1 🜨 0 🜨 0 = 1
1 & 0 v 1 & 0 v 0 & 0 = 0
Now let’s take a step to general formula. Suppose computer cell has n compartments; that *n* in modern computers varies from 4 for microcontroller to 64 for computers to 128 or even more compartments for supercomputers.
Let's have two binary numbers x, y, z and mark their digits by variables xn … x1 , yn … y1 , zn … z1 and auxiliary cn+1 cn … c1, (xi , yi , zi , ci ∈ {0, 1}). ci has meaning that ci+1 = 1 and ci = 0 iif (if and only if) ci + xi + yi > 1 (this is how carry operation works). Addition of two corresponding compartments of two cells is performed in two steps; i runs from 1 to n:
ci 🜨 xi 🜨 yi = zi
ci & xi v ci & yi v xi & yi = ci+1
If cn+1 ≠ 0 (means cn+1 = 1), computation encountered overflow, process is sent to another program to work with overflow.
Despite having cells with very limited number of compartments, computers can write numbers of 500 digits or more and perform arithmetic operations on them because cells can be linked by software program.
1
u/[deleted] May 25 '26
[removed] — view removed comment
1
u/[deleted] May 27 '26 edited May 27 '26
[removed] — view removed comment