Click here to Skip to main content
Click here to Skip to main content

Understanding Logic Gates

, 7 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Azim Zahir
Instructor / Trainer NIIT, India
India India
I am a trainer by profession. Currently I am working with NIIT (Mumbai, India) as a Senior Faculty. I enjoy programming as a hobby. My favorite technologies are Flash, Flex and Silverlight.
 
Of late I have developed keen interest in WPF and Windows Mobile programming.
 
Apart from computers, my favorite pastime is bicycling.

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberhari1911320-Aug-12 7:36 
GeneralRe: My vote of 5 PinmemberAzim Zahir28-Aug-12 17:20 
GeneralMy vote of 4 Pinmemberjawed.ace8-Aug-11 20:54 
GeneralRe: My vote of 4 PinmemberAzim Zahir9-Aug-11 0:17 
GeneralMy vote of 3 PinmemberAlbert Holguin7-Aug-11 16:58 
GeneralRe: My vote of 3 PinmemberAzim Zahir8-Aug-11 16:49 
GeneralRe: My vote of 3 PinmemberAlbert Holguin9-Aug-11 5:21 
GeneralRe: My vote of 3 PinmemberAzim Zahir9-Aug-11 21:33 
QuestionThoughts PinmemberPIEBALDconsult7-Aug-11 9:38 
AnswerRe: Thoughts PinmemberAzim Zahir8-Aug-11 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 7 Aug 2011
Article Copyright 2011 by Azim Zahir
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid