11,433,997 members (56,406 online)

# Understanding Logic Gates

, 7 Aug 2011 CPOL
 Rate this:

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

## Share

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

 First Prev Next
 My vote of 5 hari1911320-Aug-12 7:36 hari19113 20-Aug-12 7:36
 Re: My vote of 5 Azim Zahir28-Aug-12 17:20 Azim Zahir 28-Aug-12 17:20
 My vote of 4 jawed.ace8-Aug-11 20:54 jawed.ace 8-Aug-11 20:54
 Re: My vote of 4 Azim Zahir9-Aug-11 0:17 Azim Zahir 9-Aug-11 0:17
 My vote of 3 Albert Holguin7-Aug-11 16:58 Albert Holguin 7-Aug-11 16:58
 Re: My vote of 3 Azim Zahir8-Aug-11 16:49 Azim Zahir 8-Aug-11 16:49
 Re: My vote of 3 Albert Holguin9-Aug-11 5:21 Albert Holguin 9-Aug-11 5:21
 Re: My vote of 3 Azim Zahir9-Aug-11 21:33 Azim Zahir 9-Aug-11 21:33
 Thoughts PIEBALDconsult7-Aug-11 9:38 PIEBALDconsult 7-Aug-11 9:38
 Re: Thoughts Azim Zahir8-Aug-11 1:39 Azim Zahir 8-Aug-11 1:39
 Last Visit: 31-Dec-99 19:00     Last Update: 5-May-15 2:01 Refresh 1