Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Understanding Logic Gates

0.00/5 (No votes)
7 Aug 2011 1  
This article describes the various logic gates
Sample Image - maximum width is 600 pixels

Introduction

This article explains the various logic gates. Logic gates are the foundation for building electronic circuits. I have demonstrated the working of logic gates using a simple C# WinForms application created using Visual Studio 2005. The application shows how different logic gates respond to different signals.

Background

There are two types of electronic signals, true (on) and false (off). The true signal is represented by 1 and false signal by 0. The different logic gates are AND, OR, XOR, NOT, NAND, NOR and XNOR.

  1. AND

    The AND gate, represented as , gives a true output only when both of its inputs are true.
    The truth table of the AND gate is as follows:

    Input Signal 1 Input Signal 2 Output Signal
    0 0 0
    0 1 0
    1 0 0
    1 1 1

  2. OR

    The OR gate, represented as , gives a true output when any one of its inputs are true.
    The truth table of the OR gate is as follows:

    Input Signal 1 Input Signal 2 Output Signal
    0 0 0
    0 1 1
    1 0 1
    1 1 1

  3. XOR

    The XOR gate, represented as , gives a true output when one input is true and the other is false.
    The truth table of the XOR gate is as follows:

    Input Signal 1 Input Signal 2 Output Signal
    0 0 0
    0 1 1
    1 0 1
    1 1 0

  4. NOT

    The NOT gate, represented as , gives a true output when its input is false and false output when its input is true.
    The truth table of the NOT gate is as follows:

    Input Signal Output Signal
    0 1
    1 0

  5. NAND

    The NAND gate, represented as , is an AND gate with inverted output. It produces a true output when not all of its inputs are true.
    The truth table of the NAND gate is as follows:

    Input Signal 1 Input Signal 2 Output Signal
    0 0 1
    0 1 1
    1 0 1
    1 1 0

  6. NOR

    The NOR gate, represented as , is an OR gate with inverted output. It produces a true output when none of its inputs are true.
    The truth table of the NOR gate is as follows:

    Input Signal 1 Input Signal 2 Output Signal
    0 0 1
    0 1 0
    1 0 0
    1 1 0

  7. XNOR

    The XNOR gate, represented as , is an XOR gate with inverted output. It produces a true output when both of its inputs are true or both are false.
    The truth table of the XNOR gate is as follows:

    Input Signal 1 Input Signal 2 Output Signal
    0 0 1
    0 1 0
    1 0 0
    1 1 1

Using the Code

The application I have created is a GUI application developed in C# and it simulates the working of logic gates. I have used seven boolean variables to represent the output states of the seven gates and two images to represent the ON and OFF states.

public partial class Form1 : Form
{
	bool and, or, xor, not = true, nand = true, 
		nor = true, xnor = true;	// output states
	Image on, off;		// Images to represent the ON and OFF states.

Images are initialized in the Form_Load event. The user-defined OnOff() function checks the states of each of the seven logic gates and displays the ON image or OFF image.

private void Form1_Load(object sender, EventArgs e)
{
    on = Image.FromFile(Application.StartupPath + 
			"\\onbulb.jpg");	// Initializing the ON image
    off = Image.FromFile(Application.StartupPath + 
			"\\offbulb.jpg");	// Initializing the OFF image
    OnOff();
}

private void OnOff()			// Check output states and display images
{
    picAnd.Image = (and ? on : off);
    picOr.Image = (or ? on : off);
    picXor.Image = (xor ? on : off);
    picNot.Image = (not ? on : off);
    picNand.Image = (nand ? on : off);
    picNor.Image = (nor ? on : off);
    picXnor.Image = (xnor ? on : off);
}

The seven check functions check the input states represented by the button text and set the output states.

private void CheckAnd()		// Check input states and set output states
{
    and = ((btnAnd1.Text == "ON" && btnAnd2.Text == "ON") ? true : false);
    OnOff();
}

private void CheckOr()
{
    or = ((btnOr1.Text == "ON" || btnOr2.Text == "ON") ? true : false);
    OnOff();
}

private void CheckXor()
{
    xor = ((btnXor1.Text == "ON" ^ btnXor2.Text == "ON") ? true : false);
    OnOff();
}

private void CheckNot()
{
    not = ((btnNot.Text == "ON") ? false : true);
    OnOff();
}

private void CheckNand()
{
    nand = ((btnNand1.Text == "ON" && btnNand2.Text == "ON") ? false : true);
    OnOff();
}

private void CheckNor()
{
    nor = ((btnNor1.Text == "ON" || btnNor2.Text == "ON") ? false : true);
    OnOff();
}

private void CheckXnor()
{
    xnor = ((btnXnor1.Text == "ON" && btnXnor2.Text == "ON") ||
    (btnXnor1.Text == "OFF" && btnXnor2.Text == "OFF") ? true : false);
    OnOff();
}

The button click events are used to change the text on the buttons and call the check methods to set the output states.

private void btnAnd1_Click(object sender, EventArgs e)
{
    btnAnd1.Text = (btnAnd1.Text == "ON" ? "OFF" : "ON");	// Change Button Text
    CheckAnd();						// Set output state
}

private void btnAnd2_Click(object sender, EventArgs e)
{
    btnAnd2.Text = (btnAnd2.Text == "ON" ? "OFF" : "ON");
    CheckAnd();
}

private void btnOr1_Click(object sender, EventArgs e)
{
    btnOr1.Text = (btnOr1.Text == "ON" ? "OFF" : "ON");
    CheckOr();
}

private void btnOr2_Click(object sender, EventArgs e)
{
    btnOr2.Text = (btnOr2.Text == "ON" ? "OFF" : "ON");
    CheckOr();
}

private void btnXor1_Click(object sender, EventArgs e)
{
    btnXor1.Text = (btnXor1.Text == "ON" ? "OFF" : "ON");
    CheckXor();
}

private void btnXor2_Click(object sender, EventArgs e)
{
    btnXor2.Text = (btnXor2.Text == "ON" ? "OFF" : "ON");
    CheckXor();
}

private void btnNot_Click(object sender, EventArgs e)
{
    btnNot.Text = (btnNot.Text == "ON" ? "OFF" : "ON");
    CheckNot();
}

private void btnNand1_Click(object sender, EventArgs e)
{
    btnNand1.Text = (btnNand1.Text == "ON" ? "OFF" : "ON");
    CheckNand();
}

private void btnNand2_Click(object sender, EventArgs e)
{
    btnNand2.Text = (btnNand2.Text == "ON" ? "OFF" : "ON");
    CheckNand();
}

private void btnNor1_Click(object sender, EventArgs e)
{
    btnNor1.Text = (btnNor1.Text == "ON" ? "OFF" : "ON");
    CheckNor();
}

private void btnNor2_Click(object sender, EventArgs e)
{
    btnNor2.Text = (btnNor2.Text == "ON" ? "OFF" : "ON");
    CheckNor();
}

private void btnXnor1_Click(object sender, EventArgs e)
{
    btnXnor1.Text = (btnXnor1.Text == "ON" ? "OFF" : "ON");
    CheckXnor();
}

private void btnXnor2_Click(object sender, EventArgs e)
{
    btnXnor2.Text = (btnXnor2.Text == "ON" ? "OFF" : "ON");
    CheckXnor();
}
}
}

Points of Interest

I have used the ternary operator to check conditions instead of "if" statements in order to reduce the number of lines of code. The main executable file of the application is in the bin/Release folder. After executing the program, you can click on the buttons to set the input states and see how the different gates respond to the different input signals.

History

  • 5th August, 2011: Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here