Computers - Logic Gates

Introduction
Logic gates operate on Boolean algebra and is the fundamental reasons devices such as computers are made possible.

To demonstrate how these gates work and operate I've put together Java program that uses Boolean variables to play the role of a transistor. Abstraction plays a vital role in designing circuits and emulating the logic inside an object oriented language such as Java will hopefully remove any unnecessary complexity. These Java logic gates are designed as factory methods and only contain a function called find to calculate the appropriate output from the given inputs into the logic gate.

History
Boole's pamphlet Mathematical Analysis of Logic created a branch of mathematics using symbolic logic, which is now referred to as Boolean algebra.

What Is The Role of Logic Gates?
Logic gates are components of a circuit that takes input in the form of a binary signals and returns a signal based on the states of the incoming signals. In the relam of computers, the signal for a binary one is a high amount of voltage, around five volts, and likewise an binary zero will be a signal of a low amount of voltage, close to zero (maybe), flowing through the circuit.

Primitive Gates and Compound Gates
There are two types of gates covered in this lesson and I'm going to describe them either as a primitive gate or a compound gate. A primitive gate is a simple stand alone logic gate. On the other hand, a compound gate is a logic gate consisting of multiple logic gates constructed in a way to create a more unique output.

Primitives Gates:

 * NOT


 * AND


 * OR


 * Three-State

Compound Gates:

 * NAND (NOT AND)


 * NOR (NOT OR)


 * XOR (Exclusive OR)


 * NXOR (NOT Exclusive OR)

NOT Gate
The NOT Gate is the simplest kind logic gate and is commonly referred to as an inverter. The role of the not gate is to convert a one to a zero and vise versa. This gate only requires a single relay to create this logic gate.

Source
The Java NOT object is simple on-liner. The function toggle takes in a Boolean argument that is refereed to as bit and returns the bit's opposite state

public class NOT { private NOT{} public static boolean find(boolean bit){ return( (bit) ? false : true ); } }

Unit Test
public static void main(String[] args) { System.out.println(false + " -> " + NOT.find(false) ); System.out.println(true + " -> " + NOT.find(true) ); }

Output
false -> true true -> false

AND Gate
The AND gate is extremely useful to build interesting systems and easy to understand. The gate receives two bits and compares them. If both bits states are turned on then the gate will return an active bit else the gate will be return a bit whose state is turned off. The AND gate requires 2 relays to function.

Source
The Java object for logic gate AND makes use of a similar ternary if statement that was contained in the NOT gate to emulate an AND gate.

public class AND { private AND{} public static boolean find(boolean a0, boolean a1) { return( (a0 && a1) ? true : false); } }

Unit Test
public static void main(String[] args) { System.out.println(true + " AND " + true + " = " + AND.find(true, true)); System.out.println(false + " AND " + true + " = " + AND.find(false, true)); System.out.println(true + " AND " + false + " = " + AND.find(true, false)); System.out.println(false + " AND " + false + " = " + AND.find(false, false)); }

Output
true AND true = true false AND true = false true AND false = false false AND false = false

OR Gate
Similar to the AND gate, the OR gate is a vital component in building complex systems and is simple to understand. This gate takes in two inputs and if any of the bits are active, then the gate will return an active bit. The OR gate requires two relays to be functional.

Source
public class OR{ private OR{} public static boolean find(boolean a0, boolean a1) { return( (!a0 && !a1) ? false : true ); } }

Unit Test
public static void main(String[] args) { System.out.println(true + " OR " + true + " = " + OR.find(true, true)); System.out.println(false + " OR " + true + " = " + OR.find(false, true)); System.out.println(true + " OR " + false + " = " + OR.find(true, false)); System.out.println(false + " OR " + false + " = " + OR.find(false, false)); }

Output
true OR true = true false OR true = true true OR false = true false OR false = false

NAND Gate
The NAND logic gate is a compound gate. If you couldn't guess this gate is composed of AND and a NOT gate. The way this gate works that the output of an AND gate is calculated and this passed to the invertor. The NAND gate requires two relays to be operational.

Source
public class NAND { private NAND{} public static boolean find(boolean a0, boolean a1){ return( NOT.find( AND.find(a0, a1) )); } }

Unit Test
public static void main(String[] args) { System.out.println(true + " NAND " + true + " = " + NAND.find(true, true)); System.out.println(false + " NAND " + true + " = " + NAND.find(false, true)); System.out.println(true + " NAND " + false + " = " + NAND.find(true, false)); System.out.println(false + " NAND " + false + " = " + NAND.find(false, false)); }

Output
true NAND true = false false NAND true = true true NAND false = true false NAND false = true

NOR Gate
The NOR or NOT OR, logic gate is constructed in the similar fashion as the NAND gate. Two bits are sent into the OR gate and the output is sent into the NOT gate to invert the given output. This gate requires two relays to be functional.

Source
public class NOR{ private NOR{} public static boolean find(boolean a0, boolean a1) { return( NOT.find( OR.find(a0, a1) )); } }

Unit Test
public static void main(String[] args) { System.out.println(true + " NOR " + true + " = " + NOR.find(true, true)); System.out.println(false + " NOR " + true + " = " + NOR.find(false, true)); System.out.println(true + " NOR " + false + " = " + NOR.find(true, false)); System.out.println(false + " NOR " + false + " = " + NOR.find(false, false)); }

Output
true NOR true = false false NOR true = false true NOR false = false false NOR false = true

XOR Gate
Increase the range of potential gates we are able to construct we have to connect more than two of primitive logic gates together. The XOR or Exclusive OR gate is composed of AND gate taking in the outputs from an OR and NAND gate. This gate requires six relays to be operational, two relays for each AND, OR, and NAND gate.

Source
public class XOR{ private XOR{} public static boolean find(boolean a0, boolean a1) { return( AND.find( OR.find(a0, a1), NAND.find(a0, a1) )); } }

Unit Test
public static void main(String[] args) { System.out.println(true + " XOR " + true + " = " + XOR.find(true, true)); System.out.println(false + " XOR " + true + " = " + XOR.find(false, true)); System.out.println(true + " XOR " + false + " = " + XOR.find(true, false)); System.out.println(false + " XOR " + false + " = " + XOR.find(false, false)); }

Output
true XOR true = false false XOR true = true true XOR false = true false XOR false = false

NXOR Gate
The NXOR or NOT XOR Gate is a compound gate composed of an XOR and a NOT gate. Two bits enter the XOR gate and the output is inverted in the NOT gate. Six relays are required for this gate to be functional. Two relays for each nested gate within the XOR gate.

Source
public class NXOR { private NXOR{} public static boolean find(boolean a1, boolean a2){ return( NOT.find( XOR.find(a1, a2) )); } }

Unit Test
public static void main(String[] args) { System.out.println(true + " NXOR " + true + " = " + NXOR.find(true, true)); System.out.println(false + " NXOR " + true + " = " + NXOR.find(false, true)); System.out.println(true + " NXOR " + false + " = " + NXOR.find(true, false)); System.out.println(false + " NXOR " + false + " = " + NXOR.find(false, false)); }

Output
true NXOR true = true false NXOR true = false true NXOR false = false false NXOR false = true

Three-State Gate
Three-State or Tri-gate role is to take in two bits and return a single output that can have three different types of states. These gates can return a one or zero, along with an additional state of high impedance. You can think about this type of logic gate as a switch. If the switch is in a closed state then either a one or a zero may pass but when the switch is opened, then that part of the circuit is in a state of high impedance.

Source
The following Java object is a hack to emulate TriState logic gate using only Boolean values. To fake a state of high impedance I made the find function throw a NullPointerException when every the switch is in an open state. If you don't want to worry about exception handling when using in your own program you could try using something similar to an enumeration data-type to handle all possible states. I broke the engineering naming convention I've been using before and instead of naming the parameters a0 and a1, instead I went with bit and switch to make it more clearer which bit will play the role of the switch.

public class TriState { private TriState{} public static Boolean find(boolean bit, boolean switch){ if(bit && switch) return true; else if (!bit && switch) return false; else throw new NullPointerException("Tri-State logic gate is set at high impedance!"); } }

Unit Test
public static void main(String[] args) { System.out.println; // Buffer line for printing stack trace errors. System.out.println("Signal one: " + true + ", Signal two: " + true + " -> " + TriState.find(true, true)); System.out.println("Signal one: " + false + ", Signal two: " + true + " -> " + TriState.find(false, true)); try{ TriState.find(true, false); } 	catch(NullPointerException ex){ ex.printStackTrace; System.out.println("Signal one: " + true + ", Signal two: " + false + " -> High impedance!"); } 	try{ TriState.find(false, false); } 	catch(NullPointerException ex){ ex.printStackTrace; System.out.println("Signal one: " + false + ", Signal two: " + false + " -> High impedance!"); } }

Output
java.lang.NullPointerException: Tri-State logic gate is set at high impedance! at gates.TriState.find(TriState.java:14) at tests.TestTriState.main(TestTriState.java:15) java.lang.NullPointerException: Tri-State logic gate is set at high impedance! at gates.TriState.find(TriState.java:14) at tests.TestTriState.main(TestTriState.java:21) Signal one: true, Signal two: true -> true Signal one: false, Signal two: true -> false Signal one: true, Signal two: false -> High impedance! Signal one: false, Signal two: false -> High impedance!