Binary Subtractor
A binary subtractor computes A minus B, most often by adding A to the two's complement of B using an ordinary adder.
Subtraction as addition
Building a dedicated subtractor is rarely worthwhile. In two's complement arithmetic, A - B equals A + (NOT B) + 1. An adder can perform subtraction if each bit of B is inverted and the carry-in is forced to 1, which supplies the +1.
Add/subtract unit
Place an XOR gate on each B input controlled by a mode line M. When M = 0 the XOR passes B unchanged and the carry-in is 0, so the circuit adds. When M = 1 the XOR inverts every B bit and the carry-in is 1, so the circuit subtracts. One control line switches the whole unit.
Half and full subtractors
A half subtractor computes difference D = A XOR B and borrow Bout = (NOT A) AND B. A full subtractor also takes a borrow-in. These exist in textbooks but are seldom built, because the two's-complement adder approach reuses hardware already present.
Borrow versus carry
Direct subtraction produces a borrow, the mirror of a carry. In two's-complement addition the final carry-out has a fixed meaning instead: for A - B a carry-out of 1 indicates no borrow was needed.
In code
# 8-bit subtract via two's complement
result = (a + ((~b) & 0xFF) + 1) & 0xFF