UPI Study › Courses › Discrete Mathematics › Free lessons › Lesson 13.5 Free with a free account
The Arithmetic Logic Unit (ALU)
Discrete Mathematics · Chapter 13: Boolean Algebra and Logic CircuitsSection 13.5 · lesson 85 of 8710 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Discrete Mathematics.
Lesson complete
Earn the 4 college credits for Discrete Mathematics
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Logic Gate Design →← Karnaugh MapsNext lesson: Logic Gate Design →
In this lesson
Where a processor's computing actually happens.
- Place the ALU in the fetch-decode-execute cycle
- Read its result and status flags
- Trace addition through a ripple-carry adder
- Explain parallel bitwise logic and opcode selection
Transcript
Everything said in the video, section by section (1,493 words). Read along, or search it.
Boolean Algebra and Logic Circuits
The Arithmetic Logic Unit (ALU)
This section steps back from single gates to the unit where a processor's computing actually happens, the arithmetic logic unit, or ALU. We'll place it inside the CPU, read the status flags it produces, and trace an addition through its adder. Then we'll see why bitwise logic runs in parallel, and how one instruction code picks the output.
The Opening Problem
One Unit, Almost Every Instruction
Every time a processor runs an instruction like add these two numbers, or check whether this bit is set, the actual computation happens in one component. That component is the arithmetic logic unit. The earlier sections built the Boolean tools that make it possible. This section works at the system level. Not what a single AND gate does, but how millions of gates are organized into one unit the CPU calls on for essentially every instruction. That covers a spreadsheet's sum, a game engine's collision check, and a large language model's matrix multiplication. The gate-level wiring of the adders inside is section thirteen point six. Here, the ALU is a system, described the way a datasheet describes a chip.
13.5.1
The ALU's Role in the CPU
A CPU repeatedly runs a fetch, decode, execute cycle. It fetches the next instruction from memory, and decodes which operation that instruction specifies. Then it executes it. For anything that computes, like arithmetic, comparisons or bitwise logic, executing means routing operands into the ALU and reading back the result. Formally, an ALU takes two n-bit operands, A and B, together with an opcode that says which operation to perform. It produces an n-bit result, along with a small set of status flags that describe properties of that result.
Figure 13.5.1
The ALU in the Data Path
Here's where the ALU sits in the data path. The control unit decodes the instruction's opcode, and sends it to the ALU. The register file supplies the two operands, A and B, and receives the result. The ALU computes the result and its status flags. And those flags feed back to the control unit, which uses them to decide whether any conditional branch should be taken.
Status Flags
4 Bits About Every Result
Four status flags are standard on nearly every real ALU. The zero flag, Z, is set when the result is all zeros. The carry flag, C, is set when an addition carries out of the most significant bit, or a subtraction needs a borrow. The overflow flag, V, is set when a signed result is too large to represent correctly. And the sign flag, N, is simply a copy of the result's most significant bit. The control unit reads these flags to decide, for instance, whether a jump if equal, or jump if greater, instruction actually jumps.
Worked Example
ADD R1, R2, R3
Here's an example with four-bit registers. The instruction ADD R one, R two, R three runs. R two holds zero one zero one, which is five, and R three holds zero zero one one, which is three. The control unit decodes the ADD opcode, and routes the two registers into the ALU. Out comes one zero zero zero, which is eight. The zero flag is zero, because the result isn't zero. And the carry flag is zero, because nothing carried out of the top bit. As an unsigned number, eight fits in four bits. Now look at the top bit of the result. It's one. So the sign flag, which copies that bit, is one. And read as a signed four-bit number, one zero zero zero is minus eight. Two positive numbers added to give a negative one, so the overflow flag is set too. The source lists both of those flags as zero, but that's not right. Signed four-bit values only run from minus eight to seven, so five plus three overflows. That's the real lesson of the flags. The same four bits mean eight to an unsigned instruction, and minus eight to a signed one. The flags report on both readings.
13.5.2
Adders as Building Blocks
Binary addition works exactly like grade-school column addition. Add the least significant digits, carry into the next column if needed, and repeat. An ALU's adder is organized the same way, from two small building blocks. For now, only their inputs and outputs matter. A half adder takes two input bits, and produces a sum bit and a carry-out bit. A full adder takes three input bits, two operand bits and a carry-in, and produces a sum bit and a carry-out. A ripple-carry adder chains n full adders together. Each stage's carry-out is wired to the next stage's carry-in, so together they add two n-bit numbers.
Table 13.5.1
Tracing 0101 + 0011
Let's trace five plus three through a four-stage ripple-carry adder, with bit zero as the least significant. Stage zero adds one and one, with no carry in. The sum bit is zero, and it carries one. Stage one adds zero and one, plus that carry. Sum zero, carry one. Stage two adds one and zero, plus the carry. Again sum zero, carry one. Stage three adds zero and zero, plus the carry. Sum one, carry zero. Reading the sums from stage three down to stage zero gives one zero zero zero, which is eight. And the final carry-out, zero, becomes the carry flag.
Figure 13.5.2
A 4-Bit Ripple-Carry Adder
Here's the chain that produced that trace. Four full adders sit side by side, from F A zero on the right of the number, to F A three. The carry-in to the first stage is zero. Each stage takes its pair of operand bits from above, and passes its carry-out along to the next stage. Each stage's sum bit drops out below. And the carry-out of the last stage becomes the ALU's carry flag.
When the Sum Doesn't Fit
Carry Out, and the Cost of Rippling
What if the true sum doesn't fit? Take twelve plus seven, which is nineteen. That needs five bits. The four-bit adder still produces a four-bit result, zero zero one one, which is three, plus a final carry-out of one. That carry-out becomes the carry flag. It's the CPU's only hardware signal that the true result didn't fit in the register. This short program confirms both cases. Five plus three gives eight with no carry, and twelve plus seven gives three with a carry of one. There's a cost to rippling. Each stage has to wait for the previous stage's carry, so the delay grows with the width of the register. Wider ALUs, thirty-two or sixty-four bits, often use a carry-lookahead adder instead. It adds extra logic to compute carries more directly, trading more circuitry for lower latency.
13.5.3
Bitwise Logic Runs in Parallel
Bitwise operations, AND, OR, XOR and NOT, behave completely differently in hardware. Each output bit depends only on the input bits at that same position, never on its neighbors. So an n-bit bitwise operation is just n independent copies of one simple circuit, all working at once, with no carries passed between them. Here's a common technique called bitmasking. To keep only the lower four bits of the byte for two hundred fourteen, AND it with a mask that has ones in those positions. Each output bit is the AND of the two bits directly above it, giving zero zero zero zero zero one one zero, which is six. With no carry to wait for, all eight AND gates evaluate in parallel. That's one reason graphics pipelines and machine learning accelerators are built from vast numbers of these bit-parallel units.
13.5.4
The Opcode Picks the Output
So how does the ALU choose what to do? A real ALU computes several candidate results at once: the adder's sum, the AND, the OR, the XOR, and so on. Then a multiplexer, a circuit that outputs one of several inputs based on a selector signal, uses the opcode to pick exactly one. Here's a simplified scheme with a two-bit opcode, using A equals zero one zero one and B equals zero zero one one. Opcode zero zero selects ADD, giving one zero zero zero. Zero one selects AND, giving zero zero zero one. One zero selects OR, giving zero one one one. And one one selects XOR, giving zero one one zero. All four candidates exist inside the hardware the moment A and B arrive. Only the opcode decides which one the rest of the CPU ever sees. So the ALU is one system, not four separate circuits rewired on demand. The opcode is nothing more than a selector switch on the output.
Takeaways
One System, Many Paths
To summarize. The ALU carries out the execute step. It takes two operands and an opcode, and produces a result plus the status flags zero, carry, overflow and sign. Addition is a chain of full adders, each passing its carry-out to the next, just like carrying a digit in long addition. Bitwise logic runs as independent, parallel per-bit circuits, which is part of why it's faster than addition on wide data. And the ALU computes several candidates in parallel, letting the opcode drive a multiplexer that selects the output. Here's something to think about. A bitwise AND finishes in one gate delay, but a ripple-carry addition can take as many as n. So why would a designer accept that slower worst case? And what does the existence of carry-lookahead adders suggest about how engineers trade speed against circuit complexity?
About Discrete Mathematics at UPI Study
Discrete Mathematics (Math 230) is a self-paced online course of 87 video lessons in 13 chapters, from logic and proof to counting, probability, graph theory and trees. It carries a NCCRS credit recommendation for 4 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus Every course with PRO →
Why take it here
Why students take Discrete Mathematics with UPI Study
Where the credit goes
Universities that work with NCCRS
Discrete Mathematics carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About The Arithmetic Logic Unit (ALU)
What does the ALU do in the fetch-decode-execute cycle?
What do the zero, carry, overflow, and sign flags mean on an ALU?
How does a ripple-carry adder add two binary numbers like 0101 + 0011?
How does an ALU choose between ADD, AND, OR, and XOR, and why is bitwise logic parallel?
About the Discrete Mathematics course
How many college credits is Discrete Mathematics?
How much does Discrete Mathematics cost and what is included?
Which universities accept UPI Study credits?
How long does Discrete Mathematics take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 4 credits in Discrete Mathematics.
87 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
4 credits · NCCRSEnroll →