Skip to content

Instantly share code, notes, and snippets.

@d0k
Created January 2, 2009 17:50
Show Gist options
  • Save d0k/42615 to your computer and use it in GitHub Desktop.
Save d0k/42615 to your computer and use it in GitHub Desktop.
C program to generate LaTeX truth tables
/* ./wtab n
* creates a truth table for LaTeX with n bits
*/
#include <stdio.h>
#include <stdlib.h>
#define SIZE unsigned long
#define MAXBITS sizeof(SIZE)*8-1
int main(int argc, char *argv[]) {
SIZE i;
int j, cols;
if (argc != 2 || (cols = atoi(argv[1])) <= 0 || cols > MAXBITS) {
fprintf(stderr, "wrong column count (max %d)\n", MAXBITS);
return EXIT_FAILURE;
}
for (i = 0; i < (1 << cols); i++) {
for(j = cols-1; j >= 0; j--)
printf("%d & ", ((i & (1 << j)) >> j));
putchar('\n');
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment