0

Essentially, I have a program that has eight variables. I'm attempting to check every combination of truth values for these eight variables, and so I need a truth table using 0s and 1s that demonstrate every combination of them. These inputs will be read into the program.

It should look something like:

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 1

And so on...

How would I accomplish this in C? I've opened up a file for writing, but I'm not sure how to... Logically do this.

Noc
  • 519
  • 7
  • 18
  • If you imagine that the individual bits are binary digits of a number, can you spot a pattern? – Kerrek SB Jan 28 '13 at 18:06
  • @KalîlÁrmstrøng Oh well. I thought you just couldn't implement it... I didn't realize you didn't know these are just the natural numbers in binary. Sorry. –  Jan 28 '13 at 18:19

2 Answers2

1

Convert every decimal number till 2^8, into corresponding binary number... and you have the required pattern.....

Bharat Gaikwad
  • 530
  • 2
  • 5
  • 16
1

Two loops and the character '0' are more than enough...

FILE *f = fopen("truth.txt", "w"); // we'll alter the truth...
assert(f != NULL);

unsigned i;
int j;
for (i = 0; i < 256; i++) {
    for (j = 0; j < 8; j++) {
        fprintf(f, "%c ", '0' + ((i >> (7 - j)) & 1));
    }
    fprintf(f, "\n");
}

fclose(f);