Full Adder Half Adder Truth Table : Java : BlueJ

Objective :

In electronics, an adder or summer is a digital circuit that performs addition of numbers. In many computers and other kinds of processors, adders are used not only in the arithmetic logic unit(s), but also in other parts of the processor, where they are used to calculate addresses, table indices, and similar.
Although adders can be constructed for many numerical representations, such as binary-coded decimal or excess-3, the most common adders operate on binary numbers. In cases where two's complement or ones' complement is being used to represent negative numbers, it is trivial to modify an adder into an adder–subtractor. Other signed number representations require a more complex adder.

Half Adder :

Half Adder Logic Diagram
The half adder adds two one-bit binary numbers A and B. It has two outputs, S and C (the value theoretically carried on to the next addition); the final sum is 2C + S. The simplest half-adder design, pictured on the right, incorporates an XOR gate for S and an AND gate for C. With the addition of an OR gate to combine their carry outputs, two half adders can be combined to make a full adder.

With the help of half adder, we can design circuits that are capable of performing simple addition with the help of logic gates.
Let us first take a look at the addition of single bits.
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 1 0
These are the least possible single-bit combinations. But the result for 1+1 is 10. Though this problem can be solved with the help of an EXOR Gate, if you do care about the output, the sum result must be re-written as a 2-bit output. Thus the above equations can be written as
0 + 0 = 0 0
0 + 1 = 0 1
1 + 0 = 0 1
1 + 1 = 1 0
Here the output ‘1’of ‘10’ becomes the carry-out. The result is shown in a truth-table below. ‘SUM’ is the normal output and ‘CARRY’ is the carry-out.
INPUTS                 OUTPUTS
A             B             SUM      CARRY
0              0              0              0
0              1              1              0
1              0              1              0
1              1              0              1

Full Adder :

A full adder adds binary numbers and accounts for values carried in as well as out. A one-bit full adder adds three one-bit numbers, often written as A, B, and Cin; A and B are the operands, and Cin is a bit carried in from the next less significant stage.[2] The full-adder is usually a component in a cascade of adders, which add 8, 16, 32, etc. binary numbers. The circuit produces a two-bit output sum typically represented by the signals Cout and S, where . The one-bit full adder's truth table is:

Full Adder Logic Diagram
A full adder can be implemented in many different ways such as with a custom transistor-level circuit or composed of other gates. One example implementation is with  and .
In this implementation, the final OR gate before the carry-out output may be replaced by an XOR gate without altering the resulting logic. Using only two types of gates is convenient if the circuit is being implemented using simple IC chips which contain only one gate type per chip. In this light, Cout can be implemented as .
A full adder can be constructed from two half adders by connecting A and B to the input of one half adder, connecting the sum from that to an input to the second adder, connecting Ci to the other input and OR the two carry outputs. Equivalently, S could be made the three-bit XOR of A, B, and Ci, and Cout could be made the three-bit majority function of A, B, and Cin.

This type of adder is a little more difficult to implement than a half-adder. The main difference between a half-adder and a full-adder is that the full-adder has three inputs and two outputs. The first two inputs are A and B and the third input is an input carry designated as CIN. When a full adder logic is designed we will be able to string eight of them together to create a byte-wide adder and cascade the carry bit from one adder to the next.
The output carry is designated as COUT and the normal output is designated as S. Take a look at the truth-table.
INPUTS                 OUTPUTS
A             B             CIN         COUT    S
0              0              0              0              0
0              0              1              0              1
0              1              0              0              1
0              1              1              1              0
1              0              0              0              1
1              0              1              1              0
1              1              0              1              0
1              1              1              1              1
From the above truth-table, the full adder logic can be implemented. We can see that the output S is an EXOR between the input A and the half-adder SUM output with B and CIN inputs. We must also note that the COUT will only be true if any of the two inputs out of the three are HIGH.
Thus, we can implement a full adder circuit with the help of two half adder circuits. The first will half adder will be used to add A and B to produce a partial Sum. The second half adder logic can be used to add CIN to the Sum produced by the first half adder to get the final S output. If any of the half adder logic produces a carry, there will be an output carry.

BlueJ Program Screenshot :

Java Program Source Code :

 * The Program Prints the TruthTable for Full Adder and Half Adder. 
 * For Full Adder, Sum = x'y'z + xy'z' + x'yz' + xyz  Carry = xy + yz + xz
 * For Half Adder, Sum = x'y + xy'  Carry = xy
 * @author SHANTANU KHAN
 * @mail shantanukhan1995@gmail.com
 * @website 0code.blogspot.com
 * Program Type : BlueJ Program - Java
public class TruthTable
    public void fullAdderTable()
        boolean a,b,c,s,cr;
        int x,y,z,sum,carry;
        System.out.println(" x | y | z | c | s ");  System.out.println("---|---|---|---|---"); // TRUTH TABLE HEADER
                    a=(x==0)?false:true;    b=(y==0)?false:true;    c=(z==0)?false:true; // INITIALIZING BOOLEAN VALUES IN BINARY FORM
                    s=!a&&!b&&c||a&&!b&&!c||!a&&b&&!c||a&&b&&c;     cr=a&&b||b&&c||a&&c; // FULL ADDER SUM AND CARRY CHECK OPERATION
                    sum=(s==false)?0:1;     carry=(cr==false)?0:1; // CONVERTING BOOLEAN SUM AND CARRY TO INTEGERS
                    System.out.println(" "+x+" | "+y+" | "+z+" | "+carry+" | "+sum); // PRINTING EACH LINE OF TRUTH TABLE
    public void halfAdderTable()
        boolean a,b,s,cr;
        int x,y,sum,carry;
        System.out.println(" x | y | c | s ");  System.out.println("---|---|---|---"); // TRUTH TABLE HEADER
                a=(x==0)?false:true;    b=(y==0)?false:true; // INITIALIZING BOOLEAN VALUES IN BINARY FORM
                s=!a&&b||a&&!b;     cr=a&&b; // FULL ADDER SUM AND CARRY CHECK OPERATION
                sum=(s==false)?0:1;     carry=(cr==false)?0:1; // CONVERTING BOOLEAN SUM AND CARRY TO INTEGERS
                System.out.println(" "+x+" | "+y+" | "+carry+" | "+sum); // PRINTING EACH LINE OF TRUTH TABLE
    public static void main(String args[])
        TruthTable obj=new TruthTable();
        System.out.println("\nFull Adder Truth Table\n");  obj.fullAdderTable();
        System.out.println("\nHalf Adder Truth Table\n");  obj.halfAdderTable();