Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Article

Understanding Logic Gates

Rate me:
Please Sign up or sign in to vote.
4.54/5 (12 votes)
7 Aug 2011CPOL3 min read 59.2K   1.9K   19   11
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 Image 2, gives a true output only when both of its inputs are true.
    The truth table of the AND gate is as follows:

    Input Signal 1Input Signal 2Output Signal
    000
    010
    100
    111

  2. OR

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

    Input Signal 1Input Signal 2Output Signal
    000
    011
    101
    111

  3. XOR

    The XOR gate, represented as Image 4, 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 1Input Signal 2Output Signal
    000
    011
    101
    110

  4. NOT

    The NOT gate, represented as Image 5, 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 SignalOutput Signal
    01
    10

  5. NAND

    The NAND gate, represented as Image 6, 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 1Input Signal 2Output Signal
    001
    011
    101
    110

  6. NOR

    The NOR gate, represented as Image 7, 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 1Input Signal 2Output Signal
    001
    010
    100
    110

  7. XNOR

    The XNOR gate, represented as Image 8, 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 1Input Signal 2Output Signal
    001
    010
    100
    111

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.

C#
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.

C#
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.

C#
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.

C#
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)


Written By
Instructor / Trainer NIIT, India
India India
I am a trainer by profession. Currently I am working with iFuture Technologies(India) as a Senior Faculty. I enjoy programming as a hobby. During my career I have seen the growth and decline of many technologies, many of them being my favorites like Flash, WPF, Windows Mobile Development. Few of my current favorites are Android, Xamarin and Python, though I also like traditional and evergreen languages like PHP, C#, Visual Basic and Java.

Apart from computers, my favorite pastime is bicycling.

Comments and Discussions

 
Questionto take help for the execution of the code Pin
Member 126654118-Nov-16 22:29
Member 126654118-Nov-16 22:29 
GeneralMy vote of 5 Pin
hari1911320-Aug-12 6:36
hari1911320-Aug-12 6:36 
GeneralRe: My vote of 5 Pin
Azim Zahir28-Aug-12 16:20
Azim Zahir28-Aug-12 16:20 
GeneralMy vote of 4 Pin
jawed.ace8-Aug-11 19:54
jawed.ace8-Aug-11 19:54 
GeneralRe: My vote of 4 Pin
Azim Zahir8-Aug-11 23:17
Azim Zahir8-Aug-11 23:17 
GeneralMy vote of 3 Pin
Albert Holguin7-Aug-11 15:58
professionalAlbert Holguin7-Aug-11 15:58 
GeneralRe: My vote of 3 Pin
Azim Zahir8-Aug-11 15:49
Azim Zahir8-Aug-11 15:49 
GeneralRe: My vote of 3 Pin
Albert Holguin9-Aug-11 4:21
professionalAlbert Holguin9-Aug-11 4:21 
In my book (for clarification), a 3 is not bad, its just not good either (middle of the field). So no insult meant by the way. Smile | :)
GeneralRe: My vote of 3 Pin
Azim Zahir9-Aug-11 20:33
Azim Zahir9-Aug-11 20:33 
QuestionThoughts Pin
PIEBALDconsult7-Aug-11 8:38
mvePIEBALDconsult7-Aug-11 8:38 
AnswerRe: Thoughts Pin
Azim Zahir8-Aug-11 0:39
Azim Zahir8-Aug-11 0:39 

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

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