menuGamaTrain
search

chevron_left ALU (Arithmetic and Logic Unit): The part of the CPU that performs arithmetic calculations and logical operations chevron_right

ALU (Arithmetic and Logic Unit): The part of the CPU that performs arithmetic calculations and logical operations
Anna Kowalski
share
visibility12
calendar_month2026-02-23

ALU (Arithmetic and Logic Unit)

The mathematical powerhouse inside your processor that calculates, compares, and makes decisions.
📝 Summary: The Arithmetic Logic Unit (ALU) is the core component of the Central Processing Unit (CPU) responsible for all mathematical calculations and logical comparisons. It handles basic operations like addition and subtraction, bitwise manipulations (AND, OR, NOT), and comparison tasks (checking if a number is greater than another). Modern ALUs work at incredible speeds, processing billions of operations per second, and are fundamental to every program running on your computer, from video games to spreadsheets.

⚙️ Anatomy of the ALU: Inputs, Operations, and Outputs

To understand how the ALU works, imagine a simple calculator. You punch in two numbers (inputs), press a button like "+" (operation), and the display shows the result (output). The ALU works exactly like that, but millions of times faster and using electrical signals instead of buttons. An ALU typically has:

  • Input A and Input B: Two numbers (operands) that need processing.
  • Operation Code (Opcode): A signal that tells the ALU which operation to perform (e.g., add, subtract, compare).
  • Output: The result of the operation.
  • Status Flags: Special signals that give extra information about the result (e.g., if the result is zero, negative, or if an overflow occurred).

Let's see a simple example. If the CPU needs to add the numbers 5 and 3:

  1. The CPU loads 5 into Input A.
  2. It loads 3 into Input B.
  3. It sends the Opcode for "ADD".
  4. The ALU's electronic circuits immediately activate and produce 8 at the Output.
  5. The status flag might set the "Zero Flag" to 0 because the result is not zero.
💡 Tip: Think of the ALU as a super-fast worker inside the CPU. It doesn't know what the numbers mean (whether they are part of a game, a document, or a video), it just blindly and perfectly performs the operation the CPU tells it to do.

🧮 Arithmetic Operations: The Math Behind the Machine

The 'A' in ALU stands for Arithmetic. This is where the CPU performs all its math homework. These operations are the building blocks for more complex calculations in software. The most common arithmetic operations are:

  • Addition (ADD): Combining two numbers. In binary, this is done with simple rules like 0+0=0, 0+1=1, 1+1=10 (which is 0 with a carry of 1).
  • Subtraction (SUB): Finding the difference between two numbers. ALUs often perform subtraction by converting the second number to its 'two's complement' (a way to represent negative numbers) and then adding it to the first.
  • Multiplication (MUL): Repeated addition. In early CPUs, this was a slow process, but modern ALUs have dedicated circuits to multiply numbers quickly.
  • Division (DIV): Splitting a number into parts. This is the most complex basic arithmetic operation and often takes multiple clock cycles to complete.
  • Increment (INC) and Decrement (DEC): Adding or subtracting exactly 1 from a number. This is very common in loops (e.g., counting 1,2,3...).

🧠 Logic Operations: The Decision Maker

The 'L' in ALU is for Logic. While arithmetic works with numbers, logic works with 'truth' and 'falsehood'. In computers, this is represented by bits: 1 for TRUE and 0 for FALSE. Logic operations are performed bit-by-bit. The core logic operations are:

  • AND: The result is 1 only if BOTH input bits are 1. Think of it as 'everything must be true'.
  • OR: The result is 1 if AT LEAST ONE input bit is 1. Think of it as 'at least one must be true'.
  • NOT: This is a unary operation (it works on only one input). It simply flips the bit: 1 becomes 0, and 0 becomes 1.
  • XOR (Exclusive OR): The result is 1 if the input bits are DIFFERENT. If they are the same, the result is 0.

For example, let's perform a logical AND on two 4-bit numbers:

  • Number A: 1010 (binary for 10)
  • Number B: 1100 (binary for 12)
  • AND Result: 1000 (because we compare bit-by-bit: 1 AND 1 = 1; 0 AND 1 = 0; 1 AND 0 = 0; 0 AND 0 = 0)

⚡ How the ALU Works with the CPU

The ALU doesn't work alone. It is a team player inside the CPU. Here's a typical interaction:

  1. Control Unit (CU): This is the 'traffic cop' of the CPU. It reads a program instruction (like ADD) from the computer's memory.
  2. Fetching Data: The CU then fetches the required data from memory or from registers (small, super-fast storage locations inside the CPU).
  3. Activating the ALU: The CU sends the data (Input A and B) and the Opcode to the ALU. The ALU instantly performs the requested operation.
  4. Storing the Result: The result is sent back to a register or to memory, ready to be used by the next instruction.

This fetch-decode-execute cycle happens billions of times per second, a rhythm known as the clock speed (measured in Gigahertz, or GHz).

📱 Real-World Example: The ALU in a Calculator App

Let's imagine you are using a calculator app on your phone. You type 15 + 7 =. Here is the invisible journey:

  1. Your tap on '1' and '5' stores the number 15 in a memory location.
  2. Tapping '+' tells the app that an addition is coming.
  3. Typing '7' stores the second number.
  4. When you hit '=', the app sends a command to the CPU: "ADD the number at location A (15) and the number at location B (7)."
  5. The CPU's Control Unit wakes up the ALU, feeds it the two numbers, and tells it to add.
  6. The ALU's addition circuit activates. Using binary math, it calculates 1111 (15) + 0111 (7) = 10110 (22).
  7. The result 22 is sent back through the CPU to the app, and it appears on your screen.

This entire process, from the moment you press '=' to the result showing up, often takes less than a microsecond!

📊 Common ALU Operations

The table below summarizes the typical operations an ALU can perform. Note that different CPU architectures (like x86 or ARM) might have slightly different sets.

CategoryOperationDescriptionExample (Decimal)
ArithmeticADDAdds two numbers8 + 4 = 12
ArithmeticSUBSubtracts one number from another10 - 3 = 7
ArithmeticMULMultiplies two numbers6 * 7 = 42
LogicANDBitwise AND5 (0101) AND 3 (0011) = 1 (0001)
LogicORBitwise OR5 (0101) OR 3 (0011) = 7 (0111)
ComparisonCMPCompares two numbers (subtracts them) and sets flagsIs 5 > 3? → Yes (Zero Flag = 0, Carry Flag = 0)
Bit ShiftSHL/SHRMoves bits left (multiply by 2) or right (divide by 2)3 (0011) SHL 1 = 6 (0110)

❓ Important Questions About the ALU

Question 1: Can the ALU handle very large numbers, like a billion?
Yes, but it does it in pieces. The size of a number an ALU can handle in one go is called its word size (e.g., 32-bit or 64-bit[1]). A 64-bit ALU can directly work with numbers up to 2^64 -1 (about 18.4 quintillion). For even larger numbers, special software routines break the number into smaller chunks and ask the ALU to process them one at a time.
Question 2: What happens if the ALU tries to divide a number by zero?
The ALU cannot perform division by zero. When it detects this operation, it sets a special flag called the 'Divide Error' flag or triggers an interrupt. The operating system then usually steps in and terminates the program that requested the illegal operation, often showing an error message like "Division by zero".
Question 3: Is there more than one ALU in a modern CPU?
Absolutely! Modern CPUs (and GPUs) are packed with multiple ALUs. A quad-core processor might have 4 or more ALUs per core, allowing it to perform many calculations simultaneously. This is called parallelism and is why your computer can run many apps at once without slowing down too much.
🎓 Conclusion: The Arithmetic Logic Unit (ALU) is the unsung hero inside every computing device. From the simple addition in a calculator to the complex physics calculations in a video game, the ALU is constantly at work, performing billions of elementary operations per second. Its design, combining arithmetic and logic, perfectly mirrors the two fundamental aspects of computing: manipulating numbers and making decisions. Understanding the ALU is the first step toward understanding how a piece of metal, silicon, and plastic can think, calculate, and bring our digital world to life.

📝 Footnote

  • [1] 64-bit: Refers to the size of a data unit (word) that a CPU's registers and the ALU can process at one time. A 64-bit ALU can operate on 64 bits of data in a single instruction, allowing for more precise calculations and access to a larger amount of memory (RAM).
  • [2] Opcode (Operation Code): The portion of a machine language instruction that specifies the operation to be performed. For example, a binary pattern like '1000' might be the opcode for the ADD operation.
  • [3] Status Flags: Individual bits in a special register (often called the "FLAGS" register) that are set or cleared by the ALU after an operation to indicate the result's properties, such as Zero (ZF), Carry (CF), Overflow (OF), and Sign (SF).

Did you like this article?

home
grid_view
add
explore
account_circle