Jump to content

Printing out Truth Tables for xOR, OR, and their equivalence in C

Hello guys and fellow programmers.

 

I have a implementation of ORs and xORs that is still missing a very important key,

 

their parameters p,q,&r and incrementing the parameters properly.

 

Such that p q & r hold these values through each iteration of the for loop

 

p q r

0 0 0

0 0 1

0 1 0

0 1 1

1 0 0

1 0 1

1 1 0

1 1 1

 

I was given the pseudocode for the permutations but can't figure out it's implementation into the forloop any ideas or help would be much appreciate.

count++; p = count & 1; q = count & 2; r = count & 4;

Time for the code! :)

 

First the functions OR(implication) & xOR(biconditional)

bool implication(bool a, bool b){	if (a || b)//if a or b is true	{		return true;	}	else {return false;}}//XOR Function//It's bi condition given that if a is true then b can't be true, if b is true than a can't be true//It's either one or the other NOT BOTHbool biconditional(bool a, bool b){	if (a != b)	{		return true;	}	else{		return false;		}	//Can only have 1 not 2}

I won't post the entire source but there's a sample loop of what I'm doing to print out the truth tables

I just can't figure out how to implement the permutations for p q and r

/* TEST Bp  q  r | p and (q -> r) | (p and q) xor r0  0  0 |       0        |       00  0  1 |       0        |       10  1  0 |       0        |       00  1  1 |       0        |       11  0  0 |       1        |       01  0  1 |       1        |       11  1  0 |       0        |       11  1  1 |       1        |       0These expressions are NOT equivalent*/	printf("P  |  Q  |  R  |p and (q->r)|(p and q)<->r\n");	for (int i = 1; i <= 8; ++i)	{		printf("%d  |  %d  |  %d  |     %d      |     %d\n", p, q, r, p&&(implication(q,r), biconditional(p&&q,r)));	}	printf("These expressions are NOT equivalent\n");	printf("\n\n");

Any ideas or hints as to how I can go about this?

 

Please and thank you as always!

Link to comment
Share on other sites

Link to post
Share on other sites

I still don't like truth tables.

NCASE M1 i5-9600k  GTX 1080 FE Z370N-WIFI SF600 NH-U9S LPX 32GB 960EVO

I'm a self-identifying Corsair Nvidia Fanboy; Get over it.

Link to comment
Share on other sites

Link to post
Share on other sites

Just so I'm sure I understand you want to increment the values of p q and r like above and each time feed those values to a function that would print out the truth table results for those values? 

CPU: Intel Core i5 2500K | Case: Bitfenix Prodigy | Motherboard: GA-H61N-USB3 | RAM: Corsair 8GB 1333 MHz Video CardEVGA GTX 660 Superclocked 2GB DDR5

Power Supply: Corsair CX 430 | SSD: Samsung 840 120GB | HDD: 2X Seagate Barracuda 500GB 7200rpm | Monitor Asus PB238Q & Asus PB278Q

Mouse: Lenovo N50 | Keyboard: Apple Pro Keyboard | Operating Systems: Hackintosh OS X 10.8.5 & Windows 8.1

Link to comment
Share on other sites

Link to post
Share on other sites

EXACTOMUNDO.

 

Which leads back to the proper permutatin of p q r

 

Which then goes back to implementing the pseudocode.

 

This was a lot easier back in assembly.. >.<

Link to comment
Share on other sites

Link to post
Share on other sites

Your implication and biconditional functions seem like they would behave correctly. As for the incrementation of p q and r. I would increment an int from 0-7, change that int to its binary expression. This will be the same as the combined numbers for p q and r. Then you would just have to find a way to separate those digits so you can operate on them separately. You can use a bit mask. If you don't know what that is. This is an example

 

 

Say I have the bit string     0 1 1   and I only wanted to know what the first value is. I can do an AND operation on those bits    0 1 1   (3 & 1) = 1   Another example     1 1 1   7 & 4 = 4 ( this would be to find your p value for iteration number 7)

                                                                                                                                                                                                       0 0 1                                                      1 0 0

                                                                                                                                                                                                      --------                                                    ---------

                                                                                                                                                                                                       0 0 1                                                      1 0 0

 

This method would only bring down the first value in the string. Then if I wanted to know what value existed in the first value place I would compare it with the int value 1(if true then value is 1) or 0. The second place would be 2 (if true, then the value is 1) or 0 (if true then the value is 0). The third place would be 4(if true then the value would be 1) or 0. 

 

 

I hope that makes somewhat sense. There is probably a better and more elegant way but this is just what I came up with off the top of my head right now. Hope it helps 

 

 

EDIT: Looking back at the pseudocode you have above. What I have said is essentially that but instead of saying p = count & 4. You would need to find the bit value because that expression would not result in what you want. It would give you either a 4 or 0 (not 1 or 0). 

CPU: Intel Core i5 2500K | Case: Bitfenix Prodigy | Motherboard: GA-H61N-USB3 | RAM: Corsair 8GB 1333 MHz Video CardEVGA GTX 660 Superclocked 2GB DDR5

Power Supply: Corsair CX 430 | SSD: Samsung 840 120GB | HDD: 2X Seagate Barracuda 500GB 7200rpm | Monitor Asus PB238Q & Asus PB278Q

Mouse: Lenovo N50 | Keyboard: Apple Pro Keyboard | Operating Systems: Hackintosh OS X 10.8.5 & Windows 8.1

Link to comment
Share on other sites

Link to post
Share on other sites

Perfect, thank you for the explanation!

 

Just to update on my progress:

Here's what I have so far.

    int p = 0, q = 0, r = 0;    //count++; p = count & 1; q = count & 2; r = count & 4;    for (int count = 0; count <= 7; count++)    {        r = count & 1;        q = (count & 2)/2;        p = (count & 4)/4;        printf("p:%d q:%d r:%d\n", p, q, r);    }

Next step is just to get the bit values instead of the decimals.

 

Thanks again!

 

[EDIT] - Got the correct permutation and list of values :)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×