Find a course

Module 2: Logic Gates and CircuitsLesson 5 of 1113 min

From Gates to Adders and Memory

Add binary numbers by hand, build half and full adders from gates, see how overflow happens, and learn how flip-flops and the clock let circuits remember.

Step 1 of 15Learn

Adding in binary

Binary addition follows the same column method you learned in school, with a smaller set of facts:

  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 1 = 10 (that's two: write 0, carry 1)
  • 1 + 1 + 1 = 11 (three: write 1, carry 1)

Add 0110 (6) and 0111 (7), from the right:

  • Ones column: 0 + 1 = 1, no carry.
  • Twos column: 1 + 1 = 10, write 0, carry 1.
  • Fours column: 1 + 1 + carry 1 = 11, write 1, carry 1.
  • Eights column: 0 + 0 + carry 1 = 1.

Result: 1101, which is 13. Correct, since 6 + 7 = 13.

The whole job in each column is: take two bits plus a carry, produce a sum bit and a new carry. That fixed rule can be built from gates, which means hardware can add.

Step 2 of 15Practice · Work it out

Add the binary numbers 1011 and 0110. Type the binary result.

Step 3 of 15Learn

Half adders and full adders

Look at one column with two input bits, A and B:

ABSumCarry
0000
0110
1010
1101

Compare with the gate truth tables. The Sum column is exactly A XOR B. The Carry column is exactly A AND B. So one XOR gate and one AND gate add two bits. This circuit is a half adder.

It's half because it ignores a carry coming in from the column to the right. A full adder takes three inputs (A, B and carry-in) and outputs a sum and a carry-out. It can be built from two half adders and an OR gate.

Chain full adders side by side, with each carry-out feeding the next carry-in, and you get a ripple-carry adder. Eight full adders add two bytes; 64 full adders add two 64-bit numbers. Real processors use faster designs that compute carries in parallel, but the principle is the same: addition is just gates.

Step 4 of 15Practice · Fill in the blanks

Fill in the blanks

In a half adder, the sum bit comes from an gate and the carry bit comes from an gate.

Step 5 of 15Diagram

A half adder

Two gates add two bits.

A half adderParts diagram: Half adder (A, B), XOR gate: Sum bit, AND gate: Carry bit.Half adder (A,B)XOR gate:Sum bitAND gate:Carry bit

Tap a shape with to see more.

Half adder (A, B)
XOR gate: Sum bit

1 when exactly one input is 1.

AND gate: Carry bit

1 only when both inputs are 1: 1 + 1 = 10.

Step 6 of 15Learn

Overflow: when the bits run out

A circuit has a fixed number of bits. An 8-bit adder can only hold results from 0 to 255. What happens when you add 200 + 100?

The true answer, 300, needs 9 bits (100101100). The adder keeps only the lowest 8 bits, 00101100, which is 44. The extra carry falls off the end. This is called overflow, and the processor sets a flag so software can notice if it cares.

Overflow has caused real failures. Software that counts seconds in a signed 32-bit number can only reach about 2.1 billion. Systems that count seconds since January 1, 1970 in such a number run out in January 2038, a problem engineers have been fixing for years by switching to 64-bit time values. Old arcade games famously glitched when level counters wrapped around.

Negative numbers usually use a scheme called two's complement, where the top bit indicates the sign. An 8-bit signed value runs from −128 to 127. Add 1 to 127 and you get −128, another form of overflow that surprises programmers who forget the limits of their number types.

Step 7 of 15Diagram

An 8-bit counter wraps around

An 8-bit counter wraps aroundCycle diagram: 253 → 254 → 255 → 0 → 1.Like a carodometer25325425501

Tap a shape with to see more.

253
254
255

11111111: every bit is 1.

0

Add 1 and the carry falls off the end: 00000000.

1

Step 8 of 15Note

Step 9 of 15Practice · Work it out

An 8-bit unsigned counter holds 250. It is increased by 10. What value does it hold afterward?

Step 10 of 15Learn

Memory from a loop of gates

Gates react to their current inputs and forget the past. To remember, a circuit must feed its output back into its own input.

The simplest example is an SR latch, made of two NOR (or NAND) gates whose outputs cross-connect to each other's inputs. Pulse the Set input and the output becomes 1 and stays 1 even after the pulse ends, because the loop keeps reinforcing itself. Pulse Reset and it flips to 0 and stays there. One latch stores one bit for as long as the power is on.

Computers refine this into the flip-flop, which only changes its stored value at a precise moment marked by a clock signal, a steady square wave of high and low pulses. Clocking keeps billions of circuits in step, so that every part finishes its work before the next tick reads the results.

Group 8, 32 or 64 flip-flops together and you have a register, a tiny fast storage slot inside the processor. Main memory (RAM) uses a different, denser design, a capacitor that holds a charge, but it serves the same purpose: keeping bits until they're needed.

Step 11 of 15Tap to reveal

Two kinds of RAM cells

  1. SRAM (static RAM)

    Built from flip-flop style circuits of about six transistors per bit. Very fast and holds its value as long as power is on, but takes a lot of chip area. Used for registers and caches inside the processor.

  2. DRAM (dynamic RAM)

    Stores each bit as charge in a tiny capacitor with one transistor. Much denser and cheaper per bit, so it is used for main memory. The charge leaks away within milliseconds, so the memory controller refreshes every row many times per second.

  3. What they share

    Both are volatile: switch off the power and the contents are lost. That's why you must save files to storage such as an SSD, which keeps data without power.

Step 12 of 15Practice · Put in order

Put these building blocks in order from smallest to largest.

Drag the items, or use the arrow buttons, to put them in order.

  1. Transistor
  2. Arithmetic logic unit (ALU)
  3. Logic gate
  4. Full adder
  5. 64-bit adder

Step 13 of 15Diagram

From switches to a processor

From switches to a processorPyramid diagram: Transistors, Logic gates, Adders and latches, ALU and registers, Processor core.TransistorsLogic gatesAdders and latchesALU and registersProcessorcore
Transistors
Logic gates
Adders and latches
ALU and registers
Processor core

Step 14 of 15Note

Step 15 of 15Practice · What would you do?

What would you do?

A game stores a player's coins in an 8-bit unsigned number. Players report that after collecting their 256th coin, their total suddenly shows 0. As the developer, what is the best fix?