Created
July 29, 2014 15:20
-
-
Save hdf/a904222594bc6c113ddd to your computer and use it in GitHub Desktop.
NAND map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NAND map | |
// | |
#include <stdio.h> | |
#include <stdlib.h> | |
static char* ToBits(long long x, char bits) { | |
if (bits < 1 || bits > 64) | |
return 0; | |
char* ret = malloc(bits+1); | |
char i; | |
long long i2; | |
for (i2=1LL<<bits-1,i=0; i2>0; i2>>=1,i++) | |
ret[i] = (((x & i2) == i2) ? '1' : '0'); | |
ret[i] = 0; | |
return ret; | |
} | |
void main(int argc, char** argv) { | |
int res, i, i2; | |
char bits = 4; | |
int matrixSize = 10; | |
int matrixSize2; | |
if (argc > 1) { | |
if ((strcmp(argv[1], "h") == 0) || (strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "/h") == 0) || (strcmp(argv[1], "help") == 0) || (strcmp(argv[1], "--help") == 0) || (strcmp(argv[1], "/help") == 0)) { | |
printf("Usage:\n nand [bits [matrix_size(1) [matrix_size2]]]\n"); | |
return; | |
} | |
bits = atoi(argv[1]); | |
} | |
if (argc > 2) | |
matrixSize = atoi(argv[2]); | |
matrixSize2 = matrixSize; | |
if (argc > 3) | |
matrixSize2 = atoi(argv[3]); | |
for (i = 0; i < matrixSize; i++) { | |
for (i2 = 0; i2 < matrixSize2; i2++) { | |
res = ~(i & i2); | |
printf("%i NAND %i = %i ", i, i2, res); | |
printf("(%s NAND %s = %s) \t", ToBits(i, bits), ToBits(i2, bits), ToBits(res, bits)); | |
res = (i & i2); | |
printf("%i AND %i = %i ", i, i2, res); | |
printf("(%s AND %s = %s)\n", ToBits(i, bits), ToBits(i2, bits), ToBits(res, bits)); | |
} | |
if (i != matrixSize-1) | |
printf("\n"); | |
} | |
} | |
// make.bat: | |
// gcc nand.c -o nand.exe -s | |
// nand > nand.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment