Redstone circuits/Logic

A logic gate can be thought of as a simple device that will return a number of outputs, determined by the pattern of inputs and rules that the logic gate follows. For example, if both inputs in an AND gate are in the 'true'/'on'/'powered'/'1' state, then the gate will return 'true'/'on'/'powered'/'1'.

There are many different kinds of logic gate, each of which can be implemented with many different designs. Each design has various advantages and disadvantages, such as size, complexity, speed, maintenance overhead, or cost. The various sections will give many different designs for each gate type.

Concepts
The output of each logic circuit reflects the state of its inputs at all times (though possibly with some delay incurred by the circuit).


 * Swapping inputs


 * For most of these gates, you can swap A and B without changing the output.


 * Swapping the inputs of the IMPLIES gate will affect its output, and the NOT gate has only one input.


 * Stacking inputs


 * The AND, OR, and XOR gates can each be used in arrays to perform their operation on more than two inputs, by combining two inputs at a time, then combining the results with each other and/or other inputs. For these gates, the order in which the inputs are combined doesn't matter.


 * When an XOR gate is combined in this way, its output is on when an odd number of inputs is on.


 * Choosing a logic gate


 * When unsure which logic gate to use, try building a table like the one on the right but with just one row of outputs. List the known inputs coming in and the possible combinations of power, and for each combination write down what the output should be for the structure to work. Then compare that to the table on the right and see which gate matches the desired outputs.


 * If the the output needs to change when the input is stable, or needs to be remembered after the input has ended (things that logic gates can't do), you may also need to look at pulse circuits or memory circuits.

Logic gate
A logic gate is a basic logic circuit.

NOT Gate
A NOT Gate (aka "inverter") turns on if its input is off.


 * Torch Inverter


 * 1-wide, flat (horizontal only), silent, tileable
 * circuit delay: 1 tick


 * The torch inverter is the most commonly used NOT gate, due to it small size, versatility, and easy construction.


 * One drawback of the torch inverter is that it will "burn out" if run on a clock cycle faster than a 3-clock (3 ticks on, 3 ticks off). A burnt out torch inverter will turn back on after a random time, which may produce undesired pulses in a circuit.


 * Subtraction Inverter


 * flat, silent
 * circuit delay: 1 tick


 * The subtraction inverter offers little advantage over the torch inverter except that it can run on a 2-clock cycle without burning out. Faster clocks will not work though -- the comparator simply won't react to them.


 * Variations: The powered lever can be replaced with another always-on power component (e.g., redstone torch, block of redstone), or with a full container if a power component would be inconvenient in that location.


 * The repeater is required to ensure the input signal is strong enough to overcome the comparator's rear source, but can be removed in a number of ways. If the input power level is known (because the circuit design is fixed, so it can be calculated), the repeater can be removed by replacing the powered lever with a container which will produce the same power level. Alternatively, the repeater can be removed if the output continues to a length of redstone wire which will reduce the subtracted signal enough that the signal is inverted eventually.


 * Instant Inverter


 * instant
 * circuit delay: 0 ticks


 * The instant inverter is a basic building block of larger instant circuits.


 * The "ground" version has the largest volume, but is shorter and fits easily with flatter circuits. The "tall" version (not actually tallest) is the smallest in volume and resources, but has an input and output at inconvenient locations (though not necessarily for "in-the-sky" builds). The "long" version is larger but has input and output at a convenient ground level.


 * With the "tall" version, it can be tempting to take the output from the lower block below the slabs, but that output is only instant on the input's rising edge.


 * Behavior (i.e., how it works): An instant inverter has two sticky pistons -- one to move a block to cut off the output, and one to move a block of redstone power source.


 * When the output is off, the block of redstone powers the output. When the input turns on, the block of redstone is immediately moved, cutting off the output's power (instantly inverting the input). At the same time, the redstone repeater gets powered, but before it can power the output, the other block has moved into place to cut off the output.


 * While the input is on, the redstone repeater is trying to power the output, but the output is cut off by the moving block. As soon as the output turns off, the block starts to retract, immediately allowing the power through (instantly inverting the input). The repeater will only continue to power the output for 2 tick before it turns off, but by that time the block of redstone has arrived back in its original position to continue powering the output.


 * Variations: When fitting an instant inverter into a larger build which may require the inputs or outputs to be moved around, there are three components that need to be powered simultaneously: the two sticky pistons and the redstone repeater. Getting the input line to all three can be tricky. The "tall" version solves this problem by placing all three components next to a single input block, while the other versions need to split the input line to reach all components.


 * In addition, the block of redstone and its sticky piston can be moved around, as long as it is powered without delay by the input, and powers the output wire after the repeater and before the dust-cut in its retracted state only (it shouldn't power anything in the circuit when extended). The three circuits shown below illustrate ways of powering the output line from above, below, and from the side.

OR Gate
An OR Gate turns on if either input is on.

NOR Gate
A NOR Gate turns on if both inputs are off.

AND Gate
An AND Gate turns on if both inputs are on.

NAND Gate
A NAND Gate turns on if either input is off.

XOR Gate
An XOR Gate turns on if the inputs are different from each other (one on, the other off).

The XOR gate is useful for controlling a mechanism from multiple locations. When controls (such as levers) are combined in an XOR gate, toggling any control will toggle the XOR gate's output (like a light bulb controlled by two light switches -- you can flip either one to turn the light on or off).


 * Subtraction XOR Gate
 * flat, silent


 * This circuit is fast, silent, and easy to remember.


 * Each input is the same distance to the rear and side of the comparator closest to it, so will suppress its own signal there, but travels farther to get to the side of the further comparator, so won't suppress its signal in the further comparator. Only if both inputs are on will both comparators get suppressed by a side input.


 * However, that is only true if the inputs are the same power level (or at least not different by more than 1), otherwise one signal could overwhelm the other's attempt to suppress its signal. If this circuit is sure to receive inputs of the same power level (because the system it's part of has been designed that way), then the "basic" version can be used. Otherwise, some method should be used to ensure the inputs are equal -- for example, with repeaters (the "repeated" version) or with torches (the "inverted" version).



XNOR Gate
An XNOR Gate turns on if both inputs are the same (both on, or both off).

IMPLIES Gate
An IMPLIES Gate turns on if both inputs are on, or if the first input is off.