A 4-bit arithmetic logic unit

The problem

Build a software model of a small arithmetic logic unit (ALU), made only from gate functions, that adds, subtracts, ANDs, ORs and compares two 4-bit numbers, and reports the flags.

Requirements

Technologies

Step by step

  1. Write the single-bit gate functions and test them against their truth tables.
  2. Build the full adder from gates, and chain four of them into an adder.
  3. Add the XOR gates and the carry in to make the subtractor.
  4. Add the multiplexer that selects ADD, SUB, AND or OR, and compute the flags.
  5. Test the ALU on all 256 pairs of inputs against Python's own arithmetic and bit operators.

Expected result

A program in which every one of the 256 input pairs gives the right result and flags for all four operations.

How it is evaluated

Ways to improve it