Binary Subtractor

Instead of building a dedicated subtraction machine from scratch, you can convert any standard binary adder into a subtractor using two's complement representation. This tutorial shows you how to modify a 4-bit adder to calculate A minus B, as well as how to upgrade it with XOR gates so you can toggle between addition and subtraction on the fly. It is an essential building block for Redstone ALUs, calculators, and computers.

Redstone ComputingIntermediateJava Edition
Binary Subtractor video tutorial

Video: Subtraction | Logical Redstone #12 by mattbatwings

How to build it

  1. Set Up a 4-Bit Binary Adder

    Start with a working 4-bit binary adder (such as a Carry Cancel Adder). Designate the most significant bit (the 4th bit) on both inputs and the output as the sign bit—0 for positive numbers and 1 for negative numbers.

  2. Invert the B Inputs (Permanent Subtractor)

    To subtract B from A (A + negative B), move the input levers for the B side back by a couple of blocks. Place a Redstone Torch on the back of each B input block so every bit of B is inverted before entering the adder.

  3. Power the Carry-In Line

    Locate the Carry-In input on the least significant bit (1s place) of your adder. Power this line permanently with a lever or Redstone Torch to add 1 to the inverted B input, completing the two's complement conversion.

  4. Build XOR Gates on the B Inputs (Toggleable Version)

    If you want your machine to switch between adding and subtracting, remove the simple inverters from Step 2. Instead, build an XOR gate in front of each bit of the B input so the B levers feed into one side of each XOR gate.

  5. Connect the Subtract Control Line

    Run a shared Redstone line connected to a master 'Subtract' lever into the second input of all four XOR gates on the B side. When this line is off, B passes through unchanged; when on, every bit of B is inverted.

  6. Link the Control Line to Carry-In

    Branch the same master 'Subtract' lever line over to the adder's Carry-In input. Now, flipping the master lever ON simultaneously inverts all bits of B and adds 1 via Carry-In, turning your adder into a subtractor.

How it works

This circuit relies on Two's Complement representation, which allows binary adders to handle negative numbers without separate subtraction hardware:

  • Sign Bit: In a 4-bit system, the top bit (normally worth 8) acts as the sign bit. A 0 indicates a positive number (ranging from 0 to +7), and a 1 indicates a negative number.
  • Multiplying by -1: To turn a positive binary number into its negative counterpart, you perform two steps: invert all the bits (flip 0s to 1s and 1s to 0s) and add 1 to the result. For example, +5 (0101) inverted becomes 1010, and adding 1 yields 1011 (-5).
  • A - B as A + (-B): By placing Redstone Torches on the B input lines, the circuit automatically inverts B. By turning on the adder's Carry-In line on the first bit, the circuit adds 1. Together, the adder calculates A + (~B + 1), which equals A - B.
  • Controlled Inversion with XORs: In the toggleable version, an XOR gate outputs 1 only when its inputs differ. Feeding the control line into one side of the XOR means that when the control is 0, the B bit passes through normally (B XOR 0 = B). When the control is 1, the XOR acts as an inverter (B XOR 1 = NOT B) while also powering the Carry-In.

Tips & variations

  • Watch for Overflow: With 4 bits in two's complement, one bit is reserved for the sign, meaning the maximum positive number you can represent is +7 (0111). Adding 5 + 3 will overflow into the sign bit (1000) and read as a negative number. Expand your adder to 8 bits if you need larger numbers.
  • Consistent Bit Width: Always pad smaller numbers with leading zeros up to your full bit width before inverting them, otherwise the sign bit will not flip properly.
  • Reading Negative Outputs: Negative outputs in two's complement do not look like standard binary magnitudes (e.g., 1101 is -3). To verify a negative output by hand, invert all its bits and add 1 to reveal its positive magnitude.

Frequently asked questions

Why does my 4-bit subtractor give a negative answer for 5 + 3?
In 4-bit two's complement, the 4th bit is reserved as the sign bit (0 for positive, 1 for negative). This leaves only 3 bits for magnitude, making +7 (0111) the maximum positive value. Reaching 8 (1000) overflows into the sign bit.
How do you convert a positive binary number to a negative number?
Use two's complement: invert every bit (change all 0s to 1s and 1s to 0s) and then add 1 to the resulting binary number.
Why use XOR gates instead of Redstone Torches on the B input?
Redstone Torches permanently invert the B input so the machine can only subtract. XOR gates act as controllable inverters, allowing you to switch the circuit between addition and subtraction with a single lever.

Components used

More adder