Toggleable Adder and Subtractor

Instead of building a separate circuit for binary subtraction, you can upgrade an existing redstone adder to handle both addition and subtraction. By using Two's Complement representation, this modification lets you switch between adding (A + B) and subtracting (A - B) with the flick of a single control lever. It is an essential building block for Minecraft calculators, ALUs, and redstone computers.

Redstone ComputingIntermediateJava Edition
Toggleable Adder and Subtractor video tutorial

Video: Redstone Binary Subtraction - LRR #5 by mattbatwings

How to build it

  1. Set Up a Multi-Bit Binary Adder

    Start with a working binary adder (such as a 4-bit Carry Cancel Adder) that has two multi-bit inputs (A and B), a Carry-In line on the first bit, and output lamps.

  2. Prepare the B Input Lines

    Clear some space along the B input wires before they enter the adder. Leave the A input wires untouched, as only the B number needs to be negated for A - B.

  3. Build an XOR Gate on Each B Bit

    Insert an XOR gate inline with each bit of the B input. Connect each B input lever into one input of its corresponding XOR gate, and run the XOR gate's output directly into the adder's B input.

  4. Wire the Shared Control Tower

    Connect the second input of every XOR gate on the B lines together into a vertical redstone tower or shared bus line so they all receive the exact same control signal.

  5. Link the Control Signal to Carry-In

    Run a redstone line from that same shared XOR control line directly into the Carry-In input of the adder's least significant bit (the 1s place).

  6. Add the Mode Toggle Lever

    Place a single control lever powering the shared control line. Leave it OFF for addition mode, or flip it ON to invert B and add 1 for subtraction mode.

  7. Test Addition and Subtraction

    Input 5 (0101) on A and 2 (0010) on B. With the mode lever OFF, the output reads 7 (0111); flip the mode lever ON and the output switches to 3 (0011).

How it works

This circuit works by converting subtraction into addition using Two's Complement binary notation, where subtracting A - B is equivalent to calculating A + (-B).

  • Two's Complement Negation: In an N-bit binary system (modulo 2N), you can negate any number by inverting all of its bits (0 becomes 1, 1 becomes 0) and adding 1.
  • Conditional Inverter (XOR Gates): An XOR gate acts as a toggleable inverter. When the control input is 0 (Addition mode), the B input bits pass straight through unchanged. When the control input is 1 (Subtraction mode), every bit of the B input is inverted (creating the One's Complement of B).
  • Carry-In Adds 1: Because the mode control lever is also wired to the adder's Carry-In line, turning on Subtraction mode simultaneously adds 1 to the least significant bit.
  • Ignoring Overflow: When the two numbers are added, any carry-out beyond the final bit (e.g., the 16s place in a 4-bit adder) is discarded, wrapping the result around in modular arithmetic to yield the exact difference.

Tips & variations

  • Fixed Subtractor Variant: If you only ever need subtraction and don't need to toggle back to addition, replace the XOR gates on the B inputs with simple Redstone Torches (NOT gates) and permanently power the Carry-In line.
  • Watch Your Number Range: In 4-bit Two's Complement, the valid signed range is only -8 to +7. Notice that the highest bit acts as the sign bit (0 for positive, 1 for negative, equivalent to a -8 place value). Adding or subtracting past this range causes an overflow error.
  • Prevent Signal Delay Mismatches: When stacking vertical XOR gates for the B inputs, make sure the control tower powers all bits evenly and repeaters don't accidentally back-feed into adjacent bit lines.

Frequently asked questions

Why do we turn on the Carry-In when subtracting?
In Two's Complement binary, negating a number requires flipping all its bits and adding 1. Powering the adder's Carry-In line is a hardware shortcut that adds 1 to the total without needing a second adder.
How do I read negative answers on the output lamps?
In Two's Complement, the most significant bit (the leftmost lamp) has a negative place value. In a 4-bit system, the place values from left to right are -8, +4, +2, and +1. For example, 1101 equals -8 + 4 + 0 + 1 = -3.
Why do we ignore the final Carry-Out bit during subtraction?
Fixed-bit adders use modular arithmetic (like a 16-hour clock for 4 bits). Negative numbers are represented as large numbers that purposefully overflow the adder and wrap around to the correct difference, so the extra overflow bit is discarded.

Components used

More adder