# Thread: Truth tables and binary

1. ## Truth tables and binary

Hi everyone, its been a while since i last posted, i started teaching myself C a while ago and i'm trying to get back to it. Been really busy at work with fun languages like Ocean!

Anyway, i like to learn by having a project and working towards it. So i thought i would try to write a program which could display a truth table given a boolean expression.

My first stumbling block has been dynamic arrays. I searched through the forums and found references to malloc(). I'm not asking these to be explained but i thought now would be a good time to ask if anyone has a better method of how to go about this? I don't know how well C deals with binary.

This is what i have so far:
Code:
#include <stdio.h>

int main()
{
//Initialise Variables
int x, i;
int max_value;
int inputs[x];
int bcd_dec;
int count;
int bit;
int reg;

x = 4; //width of array
max_value = 1;
for (i = 0; i < 2; i++)
{
max_value *= x;	//Given 'x' binary bits, max_value is all bits set to 1 +1
}

printf("max_value = %d\n", max_value);
for (i = max_value - 1; i >= 0; i--)	//for each value
{
count = max_value / 2; //MSB
printf("i = %d\n", i);
bit = 0;
reg = i;
while(count > 0)
{
printf("count = %d\n", count);	//Count corresponds to value of each bit.
printf("reg = %d\n", reg);
bit = x - bit;
printf("bit = %d\n", bit);
if (reg >= count)	//if value is greater than or equal to
//corresponding bit, set it
{
printf("reg>count");
inputs[bit] = 1;
reg = reg - count;
}
else			//or not
{
printf("reg<count");
inputs[bit] = 0;
}

printf("[%d]", inputs[bit]);
bit = bit++; 		//move to next bit
count = count / 2;	//and corresponding value

}
printf("\n");
}
}
of course this doesn't work, cause i'm stupid and didn't know you couldn't do that with arrays. This is only the part which will walk through the binary representation of 0 to 15 (for example)

Sorry for the long post. Your help would be appreciated.

Also, does anyone have any ideas for projects? Maybe someone wants to set me a not to difficult task?
Kai.

2. 1. If you know x = 4, why not just say inputs[4]?
2. I have never seen a for-loop used to compute x*x before. (Edit: especially since you're supposed to be computing 2**x instead -- the for-loop condition should stop at x, not 2; and you should be multiplying 2, not x.) (Edit edit: And of course, you don't use a for loop to compute 2**x either, you use 1 << x.)
3. And looking at what you use inputs for, why not just use a single (unsigned) int and just deal with the bits? Granted you don't get the cool array index notation, but provided you weren't wanting more than 32 bits you won't need to worry about variable array sizes.
4. I'm guessing the for-loop counting down on i is to print out 15 down to 0 in binary form? Again, if you were using a single int, you'd already have 15 (or whatever) stored in binary form and you could just use & to print it out bit-at-a-time.

Tha part where i set x is because i want the array (i.e the number of inputs) to be any size. Thats why i tried inputs[x]. I set x just because i wanted to get the gist of the program out the way before putting a section in calculate x.

The second point is a mistake on my behalf. The for loop should go to x and i should be computing to the power of 2. Messed up there.

I'm sorry, i don't know of another way to do the exponent calculation. how does 2**x work and i don't understand what you said about 1 << x. Is that a bitwise shift x bits to the left?

I thought C didn't deal with binary very well?

4. I thought C didn't deal with binary very well?
What does it mean? C is dealing only with binary data

5. Originally Posted by vart
What does it mean? C is dealing only with binary data
In fact all that COMPUTERS deal with is binary data. Many computer languages may not have a way to display/use binary data directly, but internally inside the application, the data is stored in binary form - ALWAYS.

--
Mats

6. ** is not an operator in C -- it's just another notation for exponent (from back before they invented the ^ key). (Yes, I learned Fortran once upon a time. Why do you ask?) And yes, << is bit shifting. Things are binary, so shift by 1 == multiply by 2, just like in decimal notation that we all use, shift decimal point by 1 == multiply by 10.

Anyway, if you really want the number of inputs to be arbitrary, it will take a slight bit of work, but if you're looking at an expression parser, then it is probably likely that you won't see more than 26 variables (unless you allow arbitrary subscripting like v1, ..., v87132498, etc.), and 26 bits is certainly smaller than an int these days. So you should be able to get away with a single int variable inputs.