Logic Gates - Multiplexer Help

This is a discussion on Logic Gates - Multiplexer Help within the C Programming forums, part of the General Programming Boards category; Hi, I have to make a 2:1 multiplexer in C programming for an assignment The user has to input A ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    12

    Exclamation 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

    Name:  mux2x1_ckt.gif
Views: 2402
Size:  6.0 KB

    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
    Last edited by alexmallaburn; 02-22-2012 at 11:25 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    Please indent your code properly.

    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    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))...);

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    12
    Hi laserlight, could you expand on this, and yes I'm unsure of how to call functions properly.

    Thanks for quick reply

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    12
    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);
    }

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    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

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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!

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    12
    yes, dmh2000 your correct, i've mistyped that one :/

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    12
    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

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    That is because of a typographical error. Fix it
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    12
    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; }

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Dec 2011
    Posts
    12
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program related to logic gates!
    By yellowmania in forum C Programming
    Replies: 4
    Last Post: 02-14-2012, 06:38 AM
  2. Are you the next Gates?
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 08-04-2008, 06:59 PM
  3. Bill Gates!
    By Sentral in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 06-21-2006, 12:22 AM
  4. a multiplexer/data selector question
    By axon in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-12-2003, 02:48 PM
  5. Logic Gates
    By JFK in forum C Programming
    Replies: 0
    Last Post: 10-21-2001, 05:56 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21