Think of the CPU (Central Processing Unit) as the "brain" of the computer. If the CPU is the brain, the ALU is the brilliant, high-speed calculator and decision-maker within that brain. It's the part that does the actual "thinking" in terms of math and logic.
The Arithmetic Logic Unit (ALU) is a fundamental digital circuit within the CPU that is responsible for performing two primary types of operations:
Every single calculation, from adding two numbers in a spreadsheet to determining if a character in a video game has collided with a wall, is handled by the ALU.
Let's break down its two main responsibilities.
This is the "Arithmetic" part of the ALU. It handles all the mathematical tasks that a computer needs to perform. These are primarily integer-based calculations.
Example: When your code says x = 5 + 3;
, the numbers 5 and 3 are sent to the ALU, the ALU is instructed to perform an addition, and the result (8) is sent back to be stored.
This is the "Logic" part of the ALU. These operations are used to make decisions and perform bit-level manipulation. They typically work on a bit-by-bit basis and result in a true (1) or false (0) outcome.
1010 AND 1100 = 1000
)1010 OR 1100 = 1110
)NOT 1010 = 0101
)1010 XOR 1100 = 0110
)if
statements).==
)!=
)>
)<
)<<
): 0010
(2) shifted left becomes 0100
(4).>>
): 1000
(8) shifted right becomes 0100
(4).Example: When your code says if (score > 100)
, the value of score
and the number 100
are sent to the ALU. The ALU performs a comparison (Greater Than). If the condition is true, it sets a special flag (a bit) that the CPU uses to decide whether to run the code inside the if
block.
The ALU does not work in isolation. It is directed by the Control Unit (CU) and operates on data stored in Registers.
Here’s a typical workflow:
1. Fetch: The Control Unit fetches an instruction from memory (e.g., "add the numbers in Register A and Register B").
2. Decode: The Control Unit decodes the instruction, understanding what needs to be done.
3. Execute:
* The CU sends the data from Register A and Register B to the inputs of the ALU.
* The CU sends a signal to the ALU telling it *which specific operation* to perform (e.g., it sends the "add" signal).
* The ALU performs the calculation.
* The result from the ALU is written to a designated output register (e.g., Register C).
In short, the function of the ALU is to be the computational engine of the CPU.
Without the ALU, the CPU would be able to shuffle data around but wouldn't be able to calculate, compare, or manipulate it in any meaningful way. It is truly the heart of all computation.