/** * Get 8 3 bit numbers of the command line (ie, number in range 0-7) * and output the bit from those three numbers. * Either by printing the bits to the screen or by * putting the bits into an unsigned char, then outputiong that * char to the screen every time its gets all 8 bits filled. * * This is an alternate version -- perhaps cleverer -- making better * use of masks. Works by shifting the mask rather than shifting the number. * * @author gtowell * Created: April 2021 * Modified: May 2021 * **/ #include #include int main(int argc, char const *argv[]) { unsigned char output = 0; unsigned int res = 0; int placer = 1 << 7; // or just placer = 128; for (int i = 1; i <= 8; i++) { int num = atoi(argv[i]); int mask = 1 << 2; // or just mask = 4; for (int j = 0; j < 3; j++) { int v = (num & mask); printf("%d", v); if (v) output |= placer; mask >>= 1; placer >>= 1; if (!placer) { printf("\n%d\n", output); placer = 1 << 7; output = 0; } } } printf("\n"); return 0; }