How an ALU works.
I hope you are well.
What is an ALU?
The ALU, or arithmetic and logic unit, is a component inside your computer that performs, well, arithmetic and logic.
It used to be it's own chip but now is embedded in your CPU.
To understand the ALU is to understand logic gates so we will go in-depth on those as well.
Think of binary.
It uses 1s and 0s right?
So two values.
Think of something that also uses two values.
And we need 1 more thing to complete this logic.
The absence of electricity flowing through a wire is represented as a 0 and an electric current is represented as a 1.
So we can map it all up like this:
So now that we have that established how do we make logic gates?
Well, we can connect certain transistors in a way that it creates these circuits.
The main gates we use are the following:
Let's discuss these in detail.
Above is the symbolic representation of an AND gate. We abstract away all the wires, and use this symbol.
The typical AND gate takes 2 inputs (A and B) and returns 1 output. This output (if we represented electricity as binary) is A * B.
Note that if we added, for example C, it would be the same output but it would be A B C.
The typical OR gate takes 2 inputs (A and B) and returns A + B.
But wait, you might be saying, binary has no representation of 2! Well if it has a 2 it just makes it a 1.
Again, note that if we added, for example C, it would be the same output but it would be A + B + C
The NOT gate takes 1 input and returns the opposite of it.
- 1 turns into a 0
- 0 turns into a 1
The XOR gate is very similar to the OR gate.
If only one of the inputs is 1 it returns a 1.
Again, note that if we added, for example C, it would be the same output just with 3 inputs and only 1 could be on.
How is this relevant to the ALU?
The ALU uses these gates to make circuits.
This is a half adder. It takes two inputs (A and B) and combines them. If there is a carry into the next bit though. It will send a 1 on the carry line.
This can be used to make this circuit:
Which is a full adder. It s the same as the half adder but takes 3 inputs.
And we can use that to make this:
It is an 8-bit ripple-carry adder. It can add 8 bit numbers.
If you enjoyed, upvote.
And the ALU is very complex but very cool.