15,389,740 members
Articles / Programming Languages / C#
Article
Posted 7 Aug 2011

54.6K views
19 bookmarked

# Understanding Logic Gates

Rate me:

## 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 1Input Signal 2Output Signal
000
010
100
111

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 1Input Signal 2Output Signal
000
011
101
111

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 1Input Signal 2Output Signal
000
011
101
110

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 SignalOutput Signal
01
10

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 1Input Signal 2Output Signal
001
011
101
110

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 1Input Signal 2Output Signal
001
010
100
110

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 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

## Share

 Instructor / Trainer NIIT, 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.

 First Prev Next
 to take help for the execution of the code Member 126654118-Nov-16 22:29 Member 12665411 8-Nov-16 22:29
 My vote of 5 hari1911320-Aug-12 6:36 hari19113 20-Aug-12 6:36
 Re: My vote of 5 Azim Zahir28-Aug-12 16:20 Azim Zahir 28-Aug-12 16:20
 My vote of 4 jawed.ace8-Aug-11 19:54 jawed.ace 8-Aug-11 19:54
 Re: My vote of 4 Azim Zahir8-Aug-11 23:17 Azim Zahir 8-Aug-11 23:17
 My vote of 3 Albert Holguin7-Aug-11 15:58 Albert Holguin 7-Aug-11 15:58
 Re: My vote of 3 Azim Zahir8-Aug-11 15:49 Azim Zahir 8-Aug-11 15:49
 Re: My vote of 3 Albert Holguin9-Aug-11 4:21 Albert Holguin 9-Aug-11 4:21
 Re: My vote of 3 Azim Zahir9-Aug-11 20:33 Azim Zahir 9-Aug-11 20:33
 Thoughts PIEBALDconsult7-Aug-11 8:38 PIEBALDconsult 7-Aug-11 8:38
 Re: Thoughts Azim Zahir8-Aug-11 0:39 Azim Zahir 8-Aug-11 0:39
 Last Visit: 31-Dec-99 18:00     Last Update: 8-Aug-22 9:50 Refresh 1