# How does a computer work?

I have been a computer nerd for many many years. I can program in quite a few languages, and I can even build them. I sat down with a buddy the other day and asked how a computer actually takes electricity and does something with it, and we just couldnt figure it out, and Google wasn't much help either.

I mean, how does a computer take a constant flow of electricity and turn it into 1's and 0's and then actually do something with those 1's and 0's like turn a light on for 15 seconds?

I understand gates (AND, OR, NOR, NAND, NOT) and a little about diodes, resistors and transistors, but I figured this would be the perfect place to have it explained in true laymens terms!

Can anybody point me in the right direction or give me a brief explanation?

• You might as well ask "how do bacteria work?". This is an incredibly broad question. I suggest you look at a simple processor architecture (e.g. MIPS; we were taught this one in class) and work down- and upwards from there. The questions you face along the way are probably much better suited for Stack Exchange than this one. Sep 1, 2012 at 20:48
• Elves. Tiny magic elves. Sep 2, 2012 at 6:12
– vzn
Sep 18, 2012 at 21:32
• The best way to learn how a computer really works at the lowest level, is to design a simplistic microcontroller on an fpga or manually from Nand gates. I've done it in a "Computer Architectures" class and it was really an eye-opener. It's not even hard to do if you keep everything at the bare minimum, and design a 4-bit controller with addition, bit manipulation and a conditional jump. Or even simpler, a hardware implementing a simplified Turing machine. Hm, this jut gave me an idea: learn how a Turing machine works. It will give you a good insight.
– vsz
Aug 2, 2016 at 8:28

This is a broad question that does not have an easy answer; it's a long way from electrons skittering along copper wires to rendering a website in Firefox. I will attempt to give you an overview from bottom to top and point you towards the right things to look up.

### Encoding Numbers

The basic motivation is to compute things, as in doing arithmetics¹. The first thing to look at is how to represent numbers. There have been many approaches, using decimal or ternary and I think even octal systems, but in the end, binary won out. Now we know we have to build devices that deal with two values -- let's call them $0$ and $1$.

Note that there are also multiple ways to encode numbers in binary. After you build up your first processor, you realise advantages of doing things in certain ways. Popular examples are the two-complement and IEEE floats. For starters, restrict yourself to plain natural numbers.

### Gates

Assume we use binary encoding. Think of how you learned adding in primary school and write down the same for binary numbers. As it turns out, the building blocks of Boolean algebra are already there for you; it is easy to build a basic adder (and other arithmetic functions) using logic gates.

How to build such gates is outside of the scope of computer science; eletrical engineering has provided multiple solution using e.g. tubes or transistors. Head over to Electrical Engineering Stack Exchange for questions on this.

### Clock and State

Not all gates are equally fast and not all parts of a computation have the same number of gates. Therefore, we have to take extra care that individual operations do not overtake each other. It has proven useful to use a global clock; the result of a given network of gates is the state of the output wires at the end of the cycle (which may change wildly while the gates cascade towards their individual final states).

That means that results of one cycle may have to be stored until the next cycle starts, e.g. if you wire up loops. There are a number of basic elements you can use to varying effect, all build up from gates; some are called flip-flops. Those are also used to build registers, elements that store numbers for as many clock cycles as needed.

### Architecture and Commands

Now you have a myriad of design choices to make. What arithmetic operations does your processor provide? What do your commands look like? It may be educational to look at the MIPS architecture whose early forms are easy compared to other designs. Have a look at the plans:

Original from http://ube.ege.edu.tr/~erciyes/CENG311

Essentially, it's fetching and and disassembling commands, a set of registers, an ALU and control. Commands encode which ALU operation to perform on which operands (by the number of the register they are held in), how to manipulate the program counter² or which register to load/store from/to memory³.

### Further Considerations

By now you have a working processor in the modern sense, assuming you figured out how to build a memory and a way to feed it commands. On its way to a modern machine, many choices have to be made. Here are some:

### Alternatives

The above is heavily influenced by how history turned out. In a different world with different minds, computers may work differently. In fact, there is a plenty of models of computation, some of which have advantages that make them useful as abstraction for real machines in many cases. There are also attempts to imitate the way our brains work, that is to enable neural computing, or more generally to exploit problem-solving and information-storing strategies observed in nature, most prominently DNA and quantum computing.

So maybe (hopefully?) the information above is all ancient history another 50 or 100 years from now.

1. All the fancy things we do with computers today are broken down into many small arithmetic tasks which the processor executes one by one.
2. If your model allows the program to manipulate control flow, this would be the memory address the processor gets the next instruction from. You can also conceive machines that only read a fixed set of instructions from, say, a tape. In fact, early implementations did that. No jumps meant no loops; a program was a completely unrolled/unfolded series of instructions depending on the data. Obviously, being able to use the same program for multiple input data is more powerful.
3. Assuming you have memory; your processor works fine without but can then only deal with so many values at once. Early computers did read in all their data from tapes and kept them in registers. There was no memory, let alone writable, persistent storage as we know it today.
• Wow! That deserves a clap. That was a great read. Thank you for sharing. I've certainly learnt something. Although, after reading that, I think what I want to know is more the fundamentals of electrical engineering in terms of how it physically controls the electricity. I shall post the same question to that group a bit later today. Thank you! Sep 3, 2012 at 4:59
• It occurred to me that I should probably have included the "talking to the machine part". That means designing a language humans can work with efficiently and translating it (with a compiler) to the instruction set you specified. These are core topics of computer science while the material in my answer is mostly computer engineering. Sep 18, 2014 at 9:46
• I imagine that is not a quick answer. One day I'll pay some computer engineer who builds the likes of motherboards or mobile phones, just how the fundamentals work. That is to say, how a computer actually calculates or turns a light on for 5 seconds and then off. I imagine its a lot to do with switches and capacitors. Sep 18, 2014 at 10:00
• @Christian If you want to save some money, consider buying an EE experiment box (like those for kids) which includes transistors. Switching lights or building a small adder is typically included, afaik. (Again, the Electrical Engineering folks probably know more.) Sep 18, 2014 at 10:07

The complete picture is fairly complicated. There are many layers built on top of one another that collectively implement high-level abstractions on top of electrical voltages. There is no simple explanation of how everything is put together, especially considering that computer hardware and software has evolved dramatically in the past fifty years.

If you're interested in seeing how everything is put together, you might want to check out The Elements of Computing Systems, a book that starts with NAND gates and progressively builds up a working implementation of Tetris on top of a compiler and operating system that you also build. Each piece of the computer is built on the previous one, which might help you see how everything works.

• That sounds like a perfect starting place. Will start there. Thank you very much :) Sep 1, 2012 at 22:54
• I also recommend reading CODE by Charles Petzold. Jun 18, 2013 at 20:54

The podcast Security Now! did a series on how computers work, starting with base principles.

• 233 - Let's Design a Computer (part 1)
• 235 - Machine Language
• 237 - Indirection: The Power of Pointers (not technically part of the series)
• 239 - Stacks, Registers & Recursion
• 241 - Hardware Interrupts
• 247 - The “Multi”-verse
• 250 - Operating Systems
• 254 - What We'll Do for Speed

The gaps in the numbers is because every other episode is a Listener Feedback episode so you may want to watch those too. Also between 241 and 247 they did a few episodes about SSL due to the breaking news at the time about countries compelling CA to give root certs to them.

Also EP. 248 is the "Portable Dog Killer" which does not kill dogs, but a sonic beam weapon that Steve made on his own as a child to help teach a neighbor dog not to run up to the fence line and try to attack people on the sidewalk.

• interesting! Thank you for sharing. I will have a look. Sep 3, 2012 at 5:00

A computer works by manipulating energy gradients (which we then interpret as 1's and 0's) in a consistent, defined way. It's the consistency which defines a computer, while it's the definition which separates the different processors.

Also, learn about how just two logic gates (OR, and AND) are sufficient building blocks to build up any general-purpose computer. See also, Danny Hillis and the Tinkertoy computer.

• That was an intersting read about the TinkerToy, especially the fact that it always wins!(science20.com/brain_candyfeed_your_mind/…) Sep 12, 2012 at 14:39
• However, although it gives me good insight into how a computer 'thinks' ie, by giving it every combination and the mechanics to find the next answer given the previous, it doesn't answer my original question of how to actually build a computer. Assume I am heading down to my local electrical hardware store (which has everything) to buy the fundemental bits (transitors, resistors, capacitors, gates, etc) and configure them so I have an extremely basic computer, what would I need? After all, the first computer was the size of a warehouse with bits and wires, so it must be possible! Sep 12, 2012 at 14:44
• @Christian: haha, okay, build some basic logic gates (AND/OR/NOT) some flip-flops for basic memory storage and you might be able to add two 4-bit numbers.
– user2708
Sep 12, 2012 at 17:30
• I have some flip-flops! Two pairs in fact. Sep 13, 2012 at 18:10

A computer is a large synchronous logical system, which means that

• it manipulates $$0/1$$ information in the form of electric signals using the rules of Boolean calculus,
• it operates with a (very fast) clock so that all signals are sampled at discrete moments to ensure coherence.

The $$0/1$$ information is represented by two electric voltages and the Boolean operations are implemented by means of transistors (forming digital circuits).

It has specialized circuits to retain information as long as the computer is powered (RAM, in huge amounts). It has specialized circuits to perform binary arithmetic (actually the bits are handled in groups - typically in multiples of 8 - that collectively represent an integer number). And it has specialized routing capabilities (busses) based on memory addresses, which are nothing but integers.

Last but not least, a modern computer has a program also stored in RAM, which is a list of instructions coded as integers, which is executed sequentially, typically fetching data from memory, performing some arithmetic and sending the results back to memory.

In your scenario, one could think of an implementation where the elapsed time is stored somewhere in RAM and the program instructs to

• fetch the elapsed time value,
• increment it with an adder,
• compare it to a predefined delay with a comparator,
• otherwise set a specialized signal which goes to an external switch rather than RAM.

Note that all these elements nowadays achieve a high degree of sophistication, and every sentence in this very brief overview deserves several books of explanations.

• For most applications, asynchronous computers aren't worth it or at least we don't have the tools to effectively design them. But asynchronous logic is still a thing and a clock is not a defining characteristic of a computer. Jun 7, 2020 at 9:34
• @Nobody: do you have examples of asynchronous computers ?
– user16034
Jun 8, 2020 at 6:03
• "today asynchronous circuits are typically used in a few critical parts of otherwise synchronous systems where speed is at a premium, such as signal processing circuits", see the dedicated Wikipedia page on this topic. en.m.wikipedia.org/wiki/…. Jun 8, 2020 at 7:14
• @Nobody: thanks.
– user16034
Jun 8, 2020 at 7:56

There are several ways to do a delay function or convert the binary data to decimal data or signal strength. When you input value "15", the computer displays a bitmap image of the number 15 on the screen. This is actually the binary value, at the moment the binary code of your keyboard also sent to cpu. It will be parsing the data then turning the code to ALU (arithmetic logic unit) processing unit to convert it to a pulse to DAC chip. The analog signal is now being controlling the frequency (the frequency from khz to hz) of a flip-flop circuit. Now the frequency output will be directed to the RAM, and the cpu read RAM to match if the data is "1111". So that's 4 seconds.

• I'm not sure how this answers the question. Sep 14, 2017 at 12:49
• you can see that he asked how computer flashing led for 15 seconds,and i explain how the computer create the second value to use in loop. Sep 14, 2017 at 14:07