# Logic Gates - Multiplexer Help

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-22-2012
alexmallaburn
Logic Gates - Multiplexer Help
Hi, I have to make a 2:1 multiplexer in C programming for an assignment

The user has to input A B & C (with C being the switcher)

Y is the output

Truth table
 C A B Y 0 0 0 0 0 0 1 0 0 1 0 1 0 1 1 1 1 0 0 0 1 0 1 1 1 1 0 0 1 1 1 1

**Updated the truth table, since I ..........ed up the first one

Attachment 11505

Like the image above but, S = C

We have to make it out of standard gates, (AND, OR, NOT etc)

I have done this so far, it has no errors (but doesn't return the correct numbers - BIG ERROR)

Code:

```#include<stdio.h>int And1(int a, int b); int And2(int b, int c); int Not(int c); int Or (int And1, int And2); int main (void) {     int a;     int b;     int c; printf("\nEnter Value for A: "); scanf("%i", &a); printf("\nEnter Value for B: "); scanf("%i", &b); printf("\nEnter Value for C: "); scanf("%i", &c); printf("\n\nValue from Multiplexer = %d", Or); system("PAUSE"); return 0; } int Not(int c) {     int output; if(c==0) output=1; else output=0; return (output); } int And1(int a, int Not) { int output;  if(a==1 && Not==1)   output=1; else   output=0;  return (output); } int And2(int b, int c) { int output; if (b==0 && c==0)  output=0; if (b==0 && c==1)  output=0; if(b==1 && c==0)  output=0; if(b==1 && c==1)  output=1; return (output); } int Or(int And1, int And2) { int output; if(And1==0 && And2==0)  output=0; if(And1==1 && And2==0)  output=1; if(And1==0 && And2==1)  output=1; if(And1==1 && And2==1)  output=1; return (output); }```
Any help would be much appreciated, I have looked on numerous forums and can find nothing that matches this
• 02-22-2012
laserlight

I suggest that you simplify the implementation of your "logic gate" functions, e.g.,
Code:

```int Or(int x, int y) {     return x || y; }```
It would make it so much easier to understand. Also, why are you naming your functions And1 and And2, then naming your parameters And1 and And2? It almost looks like you have a misunderstanding as to how to call functions.
• 02-22-2012
anduril462
I assume c in your program corresponds to S in your diagram. You could greatly simplify your functions using C's logical operators ! (not), && (and) and || (or). Not can just return !c. Or can just return And1 || And2. Likewise for And (you should have just one And function that performs a normal and -- invert the input using your Not function).

The problem is in the following line:
Code:

`    printf("\n\nValue from Multiplexer = %d", Or);`
You don't actually call the function there. Just using it's name is gives you a pointer to the function (i.e. it's address). You need to do something like:
Code:

`printf("Value = %d", Or(And(a, Not(c))...);`
• 02-22-2012
alexmallaburn
Hi laserlight, could you expand on this, and yes I'm unsure of how to call functions properly.

• 02-22-2012
laserlight
Quote:

Originally Posted by alexmallaburn
could you expand on this

You should indent your code to help denote blocks of scope. For example, instead of writing:
Code:

```int Or(int And1, int And2) { int output; if(And1==0 && And2==0) output=0; if(And1==1 && And2==0) output=1; if(And1==0 && And2==1) output=1; if(And1==1 && And2==1) output=1; return (output); }```
Write:
Code:

```int Or(int And1, int And2) {     int output;     if(And1==0 && And2==0)         output=0;     if(And1==1 && And2==0)         output=1;     if(And1==0 && And2==1)         output=1;     if(And1==1 && And2==1)         output=1;     return (output); }```
This way, it is easy to see what is the body of the function, and what are the bodies of the individual if statements. An indent level can consist of a number of spaces (I use 4, written by pressing tab) or a tab character.

Quote:

Originally Posted by alexmallaburn
I'm unsure of how to call functions properly.

If we use the analogy of logic gates: when you define those functions, you are just "manufacturing" the logic gates (or designing a template thereof). To actually setup the circuit, you need to plugin the inputs and output and connect the gates together: this is like an actual function call that provides the functions with arguments and uses the return value of the function called.

Refer to anduril462's post #3 for examples.
• 02-22-2012
alexmallaburn
I have updated the code

but need more help with the calling the function part, could you give a full example - much appreciated

Code:

```#include<stdio.h>int And1(int a, int b); int And2(int b, int c); int Not(int c); int Or (int And1, int And2); int main (void) {         int a;         int b;         int c; printf("\nEnter Value for A: "); scanf("%i", &a); printf("\nEnter Value for B: "); scanf("%i", &b); printf("\nEnter Value for C: "); scanf("%i", &c); printf("\n\nValue from Multiplexer = %d", Or); system("PAUSE"); return 0; } int Not(int c) {         int output;                 if(!c)                 output=1; else                 output=0;         return (output); } int And1(int a, int Not) { int output;         if(a==1 && Not==1)         output=1; else         output=0;         return (output); } int And2(int b, int c) { int output;           if(b==1 && c==1)         output=1;   else         output=0;         return (output); } int Or(int And1, int And2) { int output;         if(And1==1 || And2==1)         output=1; else         output=0;         return (output); }```
• 02-22-2012
dmh2000
are you sure your truth table is correct? the circuit looks right. take for example the second line in the table.
c = 0
a = 0
b = 1
p1 = b & c => 1 & 0 == 0
p2 = a & !c => 0 & 1 == 0
q = p1 | p2 => 0 | 0 == 0

but your truth table says it should be 1
• 02-22-2012
laserlight
Here is how I might write what you have now:
Code:

```#include <stdio.h> #include <stdlib.h> int And(int x, int y); int Not(int x); int Or(int x, int y); int multiplexer(int x, int y, int z); int main(void) {     int a;     int b;     int c;     printf("\nEnter Value for A: ");     scanf("%i", &a);     printf("\nEnter Value for B: ");     scanf("%i", &b);     printf("\nEnter Value for C: ");     scanf("%i", &c);     printf("\n\nValue from Multiplexer = %d", multiplexer(a, b, c));     system("PAUSE");     return 0; } int Not(int x) {     return !x; } int And(int x, int y) {     return x && y; } int Or(int x, int x) {     return x || y; }```
Observe my indentation and how I do not have so many unnecessary blank lines.

I have taken the liberty of renaming your functions' parameters. You need to see clearly that naming a parameter And1 does not call the And1 function, and thus having And1 and And2 is redundant.

I have also taken the liberty of declaring a multiplexer function for you and calling it. How to implement it is up to you.
• 02-22-2012
oogabooga
You only need one And function, since they're both the same.

Use the logical operators in C like this:
Code:

```int And(int x, int y) {     return x && y; }```
Note that the parameters have been named differently from the other variables (and functions) in your program. That's not entirely necessary, but may help you to see that they are entirely separate from those other entities.

Your indenting is still incorrect. First, use 4 spaces (not tabs). Second, each "block" is indented. A function body is a block, so it should be indented. Any statement controlled by an if or else (among other) statements is a block and should be indented.

EDIT: I was scooped!
• 02-22-2012
alexmallaburn
yes, dmh2000 your correct, i've mistyped that one :/
• 02-22-2012
alexmallaburn
Thank you laser light - massive help

I'm getting error C2086: 'int x' : redefinition on line 40
& error C2065: 'y' : undeclared identifier on line 42
• 02-22-2012
laserlight
That is because of a typographical error. Fix it ;)
• 02-22-2012
alexmallaburn
is it that there is two int x's - I have corrected that then i get a LNK2019 error

Code:

``` int Or(int x, int x) {     return x || y; } ```
• 02-22-2012
laserlight
Quote:

Originally Posted by alexmallaburn
is it that there is two int x's

Exactly. Rename the second one to y.

Quote:

Originally Posted by alexmallaburn
I have corrected that then i get a LNK2019 error

If you're attempting to build my example as-is, then that is to be expected: the multiplexer function has not been implemented.
• 02-22-2012
alexmallaburn
how would i go about that, I understand how i would do it using if statements - does the not function for example return 1 ?

Code:

```int Not(int x) {     return !x; } ```
How do I put the outputted data from the And gate into the Or gate

How would I put different value's into the And function

as one has the input of A & !C ----- and the other has input of B & C

Sorry for being a complete noob
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last