Computers - Decoders

Introduction
Decoders like up to their namesake and decode encoded signal.

1-to-2 Decoder
This is the most basic design of a decoder and is made up of a single NOT gate. The decoder only takes in a single input, A0, and is split across two outputs, D0 and D1. The original signal is relayed to D1 and the outgoing signal to D0 is sent through a NOT gate to inverse the signal. The logic is pretty easy to follow by looking at the truth table below.

Emulating a 1-to-2 Decoder
To structure outgoing bits, I used a simple Boolean array. The array index 0 represents the value of D0, likewise the index 1 represents D1. Similar to the logic gates, these decoders were written as factory methods.

Source
import gates.AND; import gates.NOT; public class Decoder { private Decoder{} public static boolean[] one_TO_two(boolean bit){ boolean[] decoded = { NOT.find(bit), bit }; return(decoded); }    // OMIT Decoders ... }

Unit Test
public class TestDecoder { public static void print(boolean[] bits){ for(boolean bit : bits) System.out.print(bit ? 1 : 0); System.out.print("\n"); }    public static void main(String[] args) { System.out.println("Testing 1-to-2 Decoder using a false bit."); print(Decoder.one_TO_two(false)); System.out.println("\nTesting 1-to-2 Decoder using a true bit."); print(Decoder.one_TO_two(true)); } }

Standard Out
Testing 1-to-2 Decoder using a false bit. 10 Testing 1-to-2 Decoder using a true bit. 01

2-to-4 Decoder
One-to-two decoders were pretty easy to flow, making a decoder using two inputs can get more complex.

Emulating a 2-to-4 Decoder
There are a couple of different ways to design a two-to-four decoder and I'm going demonstrate two of them. The first example is a decoder written using two NOT gates and four AND gates, and another version using two 1-to-2 decoders and four AND gates.

Non-Linked Version
Source

import gates.AND; import gates.NOT; public class Decoder { private Decoder{} // OMITTED 1-to-2 decoder public static boolean[] two_TO_four(boolean a0, boolean a1){ // Building decoded bytes boolean[] decoded = { AND.find( NOT.find(a0), NOT.find(a1) ), AND.find( NOT.find(a0), a1 ), AND.find( a0, NOT.find(a1) ), AND.find( a0, a1 ) };        return decoded; } // OMITTED decoders }

Unit Test
// OMITTED print public static void main(String[] args) { System.out.println("\nTesting 2-to-4 Decoder using a false, false."); print(Decoder.two_TO_four(false, false)); System.out.println("\nTesting 2-to-4 Decoder using a false, true."); print(Decoder.two_TO_four(false, true)); System.out.println("\nTesting 2-to-4 Decoder using a true, false."); print(Decoder.two_TO_four(true, false)); System.out.println("\nTesting 2-to-4 Decoder using a true, true."); print(Decoder.two_TO_four(true, true)); }

Standard Output
Testing 2-to-4 Decoder using a false, false. 1000 Testing 2-to-4 Decoder using a false, true. 0100 Testing 2-to-4 Decoder using a true, false. 0010 Testing 2-to-4 Decoder using a true, true. 0001

Linked Version
Source import gates.AND; import gates.NOT; public class Decoder { private Decoder{} public static boolean[] one_TO_two(boolean bit){ boolean[] decoded = { NOT.find(bit), bit }; return(decoded); }    public static boolean[] linked_two_TO_four(boolean a0, boolean a1){ // Getting vales from 1-to-2 decoder boolean[] link1 = one_TO_two(a0); boolean[] link2 = one_TO_two(a1); // Building decoded bytes boolean[] decoded = { AND.find(link1[0], link2[0]), AND.find(link1[0], link2[1]), AND.find(link1[1], link2[0]), AND.find(link1[1], link2[1]) };        return decoded; } // OMITTED Decoders }

Unit Test
// OMITTED print public static void main(String[] args) { System.out.println("\nTesting 2-to-4 Decoder using a false, false."); print(Decoder.linked_two_TO_four(false, false)); System.out.println("\nTesting 2-to-4 Decoder using a false, true."); print(Decoder.linked_two_TO_four(false, true)); System.out.println("\nTesting 2-to-4 Decoder using a true, false."); print(Decoder.linked_two_TO_four(true, false)); System.out.println("\nTesting 2-to-4 Decoder using a true, true."); print(Decoder.linked_two_TO_four(true, true)); }

Standard Output
Testing 2-to-4 Decoder using a false, false. 1000 Testing 2-to-4 Decoder using a false, true. 0100 Testing 2-to-4 Decoder using a true, false. 0010 Testing 2-to-4 Decoder using a true, true. 0001