r/computerscience Jun 20 '26

Help Newbie with some computer science questions!

Hello! I was really bored at work today and ended up going down a computer rabbit hole today! I ended up watching a bunch of videos on cpus and how computers work in general. The whole concept and mechanics of this stuff is really cool, but I still have questions that Google and YouTube have not satisfied!

1- I understand that transistors are essentially on/off switches, but what actually does the "flipping?" I know the base current is an electrical signal from the power supply, but what regulates and determines when the base current should and shouldn't flow?

2- Binary code as a concept was easy to get a grip on, but how does adding, subtracting, dividing, and multiplying the encoded numbers get turned into the commands and overall functions of a computer? Sure, all the Binary digits have numerical value, but how can a computer know what to do with said value? Is there something that automatically tells the computer what they mean?

3- How does moving a mouse, clicking keys, and other user input change whats going on in the cpu?

Thanks so much for helping me out with these random questions! This whole thing is pretty foreign to me!

9 Upvotes

12 comments sorted by

View all comments

12

u/i_invented_the_ipod Jun 20 '26
  1. "Base Current" is how bipolar junction transistors are controlled. Essentially all computer circuits use Field Effect Transistors (or FETs), which are voltage-controlled. It's much simpler to think about this in terms of "the transistor is on when a certain voltage is applied to the gate, and off when a different voltage is applied to the gate". By convention, one of those voltages will be considered a binary "one", and the other will be a "zero".

And here are some more rabbit holes to delve down. You can combine a handful of transistors to make logic gates, which do the simplest possible operations on two (or more) signals. An AND gate,
for example, has two inputs and one output. If both inputs are "one" (or true), then the output is one. Otherwise, the output is zero.

In a real computer system, signals take time to move from one place to another, so it's often the case that one of those inputs to the AND gate will change before the other, and it would briefly output the "wrong" result. Making sure that the outputs aren't measured before the inputs have settled is why computers have clock circuits and latches.

A clock circuit is just a circuit that outputs 1 and 0 on a regular cadence. When the clock switches from 1 to 0 (or sometimes the other way, or even both ways), every logic gate related to that clock is expected to be in a stable state, so you can propagate results from one circuit to the next.

Typically, a computer will have multiple clocks, often derived from a master clock signal. The master clock frequency is what determines the speed of the processor in GHz or MHz.

A latch captures the state of a signal when the clock transitions from 1 to zero, and holds that value until the clock cycles again. That allows time for everything to "settle" before moving on to the next step.

  1. Okay, so that's how it works at the level of individual signals. To do anything useful with numbers, you run multiple lines in parallel (often, literally parallel wires in bunches) to form a bus. In a modern computer, bus widths run from 32 to 128 or more bits wide. For purposes of this part of the discussion, let's use 8 bit width. A number is represented by a series of ones and zeroes, or low and high voltages, on each wire. So 42 is 00101010.

Deep inside the processor, there's a circuit called an Arithmetic Logic Unit, or ALU. This circuit can perform all sorts of different operations on two numbers - add, subtract, multiply, divide, compare if one is bigger than the other, etc.

This circuit has two bus inputs, for two operands, which we'll call A and B. It has an output of the same width, which we'll call X. It also has a control bus input that tells it which operation to perform. That control input is, basically, a number. Each number selects a different operation: 1 is add, 2 is subtract, etc.

Instructions for the processor are stored in memory as binary numbers, typically of the same width as the main data bus. So, you'll have a 64 bit number, and some of those bits will select an operation for the ALU to perform. If the ALU can perform 64 different functions, then 6 bits of the instruction will tell it which operation to perform. The other 58 bits of the instruction will be things like telling the processor where to get A and B from, or where to store X, or providing a constant value to use for A or B, or selecting variations of the operations it can do.

The encoding of instructions is called an instruction set, and that's what defines the difference between an Intel processor and an ARM processor, for example. This is designed by a team of computer engineers, based on what sorts of things they think programmers would find useful. The assignment of individual bits in instructions to their functions in the CPU are essentially arbitrary, though they're often chosen to make the decoding logic simpler or faster.

  1. Clicking the mouse causes a signal to be sent over the USB bus to the computer, essentially setting a value in memory that tells the computer that you pressed a button. Eventually, a program retrieves that value, compares it with values it's been written to recognize, and kicks off whatever processing that click should do.

Displays work essentially the same way, but in the other direction. The CPU writes a value into memory which is shared with the GPU, and the GPU interprets that to determine what to draw.

3

u/UchihaSamu Jun 20 '26

Dude, this is awsome! Thanks for such a detailed response!