Logic gates can be assembled into circuits that simulate interesting things like basic binary arithmetic (addition/multiplications). These circuits can then be assembled together to form more complex programmable systems, and simulate more complex behaviors.
> how many are there in like a pc?
For a modern PC/smartphone, a few hundred *billions*.
You can reduce numbers to binary numbers, 1’s and 0’s.
You can reduce math (+ – x / etc.) to logic operations (and, or, not, xor, nand, neg).
Therefore, if you can build a device that can do simple logic operations on very small binary numbers (1 and 1, 0 or 1, etc.), then you have a computer because you can use BILLIONS of these devices tied together to do very complicated math with very large numbers.
A logic gate is a couple transistors wired together in such a way that it will do a logic operation. If you put 5 volts on the first input wire, and 5 volts on the second input wire, you get the “and” result of 5 volts at the output wire. In this case 5 volts represents a 1, so an and gate will be doing this:
1 and 1 (on the two input wires) = 1 (on the output wire)
1 and 0 = 0
0 and 1 = 0
0 and 0 = 0
The processor in a computer uses all these gates to do complex math on large numbers. Your computer screen expects 1920 x 1080 numbers from the computer, each number telling it how much to light up each dot of color (pixel) that the screen is composed of. Your mouse sends gps coordinates (sort of, it sends number coordinates) to the computer. Your keyboard sends number codes to the computer. Your printer is like a screen actually, it will shoot out droplets of color ink based on numbers that represent how much ink to shoot at a page.
So all of it is a large scale (billions) of logic gates doing math to generate numbers, which your brain then interprets as patterns of writing on a screen, controlling the screen with a mouse, typing things into a document, and so on.
Seems like you already understood how each singular logic gate works. Well, some really smart people figured out that when you chain a bunch of logic gates together, you can do math with them.
[Here](https://www.instructables.com/2-Bit-Adder-Binary-and-Decimal-Calculator) for example is a basic science project that lets you add 2 two digit binary numbers together. You input the numbers by either sending a signal or not onto the input nodes (signal = 1, no signal = 0), and it will output in a similar fashion the sum of two numbers in binary. Expanding on that concept, people eventually figure out how to [add two 4 bits numbers](https://www.instructables.com/4-Bit-Binary-Adder-1/?amp_page=true), two 8 bits numbers; then subtraction, multiplication, etc.
Our modern phones and computers are essentially built on top of these, just way, way more complicated; but the process is practically the same. Inputs go in as binaries, a string of 0 (no signal) and 1 (signal) into a bunch of nodes, run through a vast number of logic gates, and the result is spit out as a string of binaries. Every action, every process done on a computer is just binaries being sent through a bunch of logic gates.
This reminds me of college 40 years ago when I enrolled in microprocessor technology. Building circuits with AND/NAND and OR/NOR gates. It could get frustrating and challenging going several layers deep but always fun finally getting the circuit to work!
Good luck and keep at it! I would get confused many times learning gates and microprocessors in general but was persistent to try to understand.
Learning computer logic has really help with logic in general.
Modified copypasta from an earlier post:
You have wires and electrical components. You apply a voltage going in and see what voltage comes out. That’s a logic gate. Here are some. 1 means high voltage, 0 means low.
* AND: If both are 1, the result is 1. If either one or both are 0, the result is 0.
* OR: If one or the other or both are 1, the result is 1.
* XOR (exclusive OR): If one or the other (but not both) are 1, then the result is 1.
* NOT: Operates on one bit, if it’s 1 the result is 0, if it’s 0 the result is 1.
* NAND: combined NOT and AND, outputs 1 as long as both inputs aren’t 1
For example, an AND gate is an electronic circuit that accepts two voltage inputs and has one voltage output. It has just a few electronic components (resistors and wires and stuff, you could easily make one yourself) arranged so that if both inputs are high voltage, the output is high voltage. If one or both of the inputs is low voltage, the output is low voltage. We then interpret high voltage as 1, low voltage as 0, and we can do an AND on two bits. The same works for the next two, just the electronic components are arranged a bit differently to achieve the other results. The NOT gate just inverts the one input, low to high, high to low voltage.
We can use these to do things. Let’s start building.
A half-adder gives you the sum of the two inputs plus the carry. So 1 + 1 = 0 with a 1 carry (like 5+5 is 0 with 1 carry to be 10). You make this half-adder by hooking an AND gate and an XOR gate together, that’s it. You apply high voltage to both inputs (1+1), you get high low voltage out on the add line (0) plus high voltage out on the carry line (1).
But that’s too basic. We need to add numbers we can use, notice we didn’t do anything with that carry. So let’s make a full adder which accepts the numbers to be added and can take that carry from a previous adder. This is made by combining two half-adders with another XOR gate.
Now this is still for one bit, but now we have something we can chain together since each adder accepts the carry from the previous. If you have 8 bits, you string 8 full adders together so each can get the other’s carry (carry out wire connected to the carry in wire on the next one) and output the result. Put in two 8-bit numbers (remember, this is just voltage sent to wires), and you’ll get the 8-bit result plus a carry, if any. We can now add any two numbers 0-255, and we’ll be told if we need to carry a 1.
So now we can add. Subtraction, multiplication, and division all work the same way, just a different number of those basic logic gates arranged differently.
And now we can do any math by controlling what voltages get sent where. You could do this at home with a circuit board, some transistors and resistors. Put two rows of 8 flip switches to describe the first and second input numbers (up = high voltage = 1, down = low voltage = 0) where each switch feeds high or low voltage into one of your 8 full adders. Now hook a set of lights to the output of each full adder plus another light connected to the carry output. As you flip the switches, the lights will change to represent your two added numbers.
Ta-da, you just made a basic binary adding calculator. Now instead of just adders, you could put in the components for a subtractor, a multiplier, and a divider (above), and have another switch that controls which of those is hooked up to the input switches and output lights. Now you’ve made a full-blown binary calculator.
Now let’s automate this. Let’s run your circuit board on a clock that sends a pulse of electricity every second. We can also call a particular arrangement of configuration switches a “program” that can control how the electricity flows to multiple parts of the calculator. We need memory! No problem, throw in two NAND gates connected to teach other the right way and you can set and read one bit by applying and detecting electricity. Sixteen NAND gates let you store one 8-bit byte, so instead of your calculator result just flashing lights, it can be stored in these NAND gates, and it can be used later.
Congratulations, you have a primitive computer. A modern microprocessor is the same thing, except we have billions of components and run at billions of pulses per second. Instead of transistors, resistors, and wires on a circuitboard you can touch, they are printed on silicon wafers using a type of photography, where one AND gate is only several billionths of a meter in size.
Latest Answers