# C program related to logic gates!

• 02-14-2012
yellowmania
C program related to logic gates!
Hi,
have been asked to produce a c program allowing the user to simulate combinational logic gates where the user inputs binary values for A,B and C and the output circuit is displayed. have been given a template array to use with the program which contains the binary info for AND, OR and NOT gates.
the first part of the spec tells me to Start by adding C functions for simulating additional logic gates: XOR, XNOR, NAND and NOR.

all i am looking for is how would i go about answering the first part.... adding c functions for the XOR, XNOR etc logic gates as this part is confusing me?

The template code is shown below!

Code:

#include<stdio.h>
int And(int a, int b);
int Or(int a, int b);
int Not(int a);
void main()
{
///where main body of code will go
}
int And(int a, int b)
{
int output;
if(a==0 && b==0)
output=0;
if(a==1 && b==0)
output=0;
if(a==0 && b==1)
output=0;
if(a==1 && b==1)
output=1;
return (output);
}
int Or(int a, int b)
{
int output;
if(a==0 && b==0)
output=0;
if(a==1 && b==0)
output=1;
if(a==0 && b==1)
output=1;
if(a==1 && b==1)
output=1;
return (output);
}
int Not(int a)
{
int output;
if(a==0 )
output=1;
if(a==1 )
output=0;
return (output);
}

• 02-14-2012
laserlight
I suggest that you simplify, e.g.,
Code:

int And(int a, int b)
{
return a && b;
}

Similiarly, Or and Not should be implemented using || and ! to keep it simple.

For NAND, you can implement it in terms of Not and And. Likewise for the other "gates". (Since NAND is a universal gate, you could implement the other functions in terms of Nand alone, but that's probably rather unnecessary.)
• 02-14-2012
yellowmania
'' For NAND, you can implement it in terms of Not and And. Likewise for the other "gates". (Since NAND is a universal gate, you could implement the other functions in terms of Nand alone, but that's probably rather unnecessary.) ''

• I am familiar with logic gates writing them down etc but how do i go about putting that as code? obviously a NAND gate is an AND gate into a NOT gate but how do i put that as a function in my code?

• 02-14-2012
laserlight
Quote:

Originally Posted by yellowmania
how do i go about putting that as code? obviously a NAND gate is an AND gate into a NOT gate but how do i put that as a function in my code?

Composition of function calls, e.g.,
Code:

return Not(And(a, b));
• 02-14-2012
pheininger
Quote:

Originally Posted by yellowmania
... but how do i put that as a function in my code?

LaserLight has given her usual excellent advice.

But maybe I can get you started by asking a question or two.
From your original code or template which line is the declaration of the And function?
Which lines are the definition of the And function?
What name would you give your function with the NAND operaton to be consistent with the names of the other functions given in your original code or template?