logical operation functions in c; And, Or, Xor, Not etc..

This is a discussion on logical operation functions in c; And, Or, Xor, Not etc.. within the C Programming forums, part of the General Programming Boards category; How would I set up gateAnd(), gateOr()...etc... I have never tried to replicate these operations with a program, and also ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    9

    logical operation functions in c; And, Or, Xor, Not etc..

    How would I set up gateAnd(), gateOr()...etc...

    I have never tried to replicate these operations with a program, and also first time using c, only have used Java previously..

    I also will have to setup halfAdder, and fullAdder.

    Here is the code:

    Code:
    #include "stdlib.h"
    #include "assert.h"
    #include "stdio.h"
    #include "circuit.h"
    
    int main(int argc, char * argv[])
    {		
    	struct gate * g = gateInput("Type an Integer");
    	gatePrint(g);		
    	return 0;
    }
    
    	struct gate * gateInput(char * prompt) 
    	{		
    		int i;
    		printf(prompt);
    		scanf("%d", &i);
    		return gateConstant(i);
    	}
    
    	struct gate * gateConstant (int value)
    	{
    		struct gate * r = (struct gate *) malloc(sizeof(struct gate));
    		assert(r != 0);
    
    		if (value == 0)
    		return gateZero();
    
    		if (value == 1)
    		return gateOne();
    	}
    
    	struct gate * gateZero()
    	{
    		struct gate * r = (struct gate *) malloc(sizeof(struct gate));
    		assert(r != 0);
    		r -> gateType = ConstantZeroGate;
    		return r;
    	}
    
    	struct gate * gateOne()
    	{
    		struct gate * r = (struct gate *) malloc(sizeof(struct gate));
    		assert(r != 0);
    		r -> gateType = ConstantOneGate;
    		return r;
    	}
    
    	void gatePrint (struct gate * e)
    	{
    		printf("The entered Value: %d \n",*e);
    	}
    
    	int gateEval (struct gate * e)
    	{
    		
    	}
    
    	void intToBit (int value, int * eights, int * fours, int * twos, int * ones)
    	{
    		assert ((value >= 0) && (value < 16));
    		*ones = value % 2;		
    	}
    
    	struct gate * gateNot(struct gate * arg)
    	{
    	
    
    	}
    	
    	struct gate * gateAnd(struct gate * left, struct gate * right)
    	{
    
    	}
    
    	struct gate * gateOr(struct gate * left, struct gate * right)
    	{
    
    	}
    
    	struct gate * gateXor(struct gate * left, struct gate right)
    	{
    
    	}
    
    	void halfAdder(struct gate * left, struct gate * right, struct * carryin, struct gate **sum)
    	{
    
    	}

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Last edited by Dave_Sinkula; 10-02-2006 at 10:28 PM. Reason: ...
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    134

    Thumbs up

    Hi,
    In C u can achive this by using '&&' for AND, '||' for OR, '!' for NOT, '^' for XOR, '&' bitwise AND,'|' Bitwise OR . Better refer to some "using Logical operators and bitwise operators in C". Through manipulating them you can setup Half adder and Full adder.
    Last edited by maven; 10-02-2006 at 10:30 PM.
    S_ccess is waiting for u. Go Ahead, put u there.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    9
    Thank you for the link, it's a good reference

    Thank you "maven" for the info, I was using bitwise for OR , AND and need them for integers..

    what do you think about the intToBit function?

    What do you think about this code? will these functions operate properly?

    Code:
    void intToBit (int value, int * eights, int * fours, int * twos, int * ones)
    	{
    		assert ((value >= 0) && (value < 16));
    		*ones = value % 2;
    		*twos = (int)(value/2) % 2;
    		*fours = (int)((value/2)/2) %2	
    		*eights = (int)(((value/2)/2)/2) %2	
    	}
    
    	struct gate * gateNot(struct gate * arg)
    	{
    		struct gate * r = !arg;
    		return r;
    	}
    	
    	struct gate * gateAnd(struct gate * left, struct gate * right)
    	{
    		struct gate * r = left && right;
    		return r;
    	}
    
    	struct gate * gateOr(struct gate * left, struct gate * right)
    	{
    		struct gate * r = left || right;
    		return r;
    	}
    
    	struct gate * gateXor(struct gate * left, struct gate right)
    	{
    		struct gate * r = left ^ right;
    		return r;
    	}
    Last edited by Jasonx521; 10-02-2006 at 10:37 PM.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    Read the difference between in " Logical AND '&&', Logical OR '||' " and "Bitwise AND '&', Bitwise OR '|' " respectively. Then Use them according to your need.
    S_ccess is waiting for u. Go Ahead, put u there.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    9
    Anyone with a solution?

    Here is my Errors:

    Code:
    assignment1.c: In function `gateNot':
    assignment1.c:74: warning: initialization makes pointer from integer without a cast
    assignment1.c: In function `gateAnd':
    assignment1.c:80: warning: initialization makes pointer from integer without a cast
    assignment1.c: In function `gateOr':
    assignment1.c:86: warning: initialization makes pointer from integer without a cast
    assignment1.c: In function `gateXor':
    assignment1.c:92: error: invalid operands to binary ^
    assignment1.c: At top level:
    assignment1.c:96: error: syntax error before '*' token
    And here is the code

    Code:
    struct gate * gateNot(struct gate * arg)
    	{
    		struct gate * r = !arg;
    		return r;
    	}
    	
    	struct gate * gateAnd(struct gate * left, struct gate * right)
    	{
    		struct gate * r = (left && right);
    		return r;
    	}
    
    	struct gate * gateOr(struct gate * left, struct gate * right)
    	{
    		struct gate * r = (left || right);
    		return r;
    	}
    
    	struct gate * gateXor(struct gate * left, struct gate * right)
    	{
    		struct gate * r = (left ^ right);
    		return r;
    	}

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    Hi,

    Here Its a limitation of C, that you can not make basic operations through predefined operators on User defined variables. As structure leads to a userdefined variable. extract out predefined datatypes from structure and then apply these basic operations on them.
    S_ccess is waiting for u. Go Ahead, put u there.

Popular pages Recent additions subscribe to a feed

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