all repos — nand2tetris @ fb4ae27aa022f203ced97d453d450b41687eface

my nand2tetris progress

projects/06/bin2text.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
#include <stdio.h>
#include <stdint.h>

void output_binary(uint8_t b)
{
	char i, msb;

	for (i = 0; i < 8; ++i) {
		msb = b & 0x80;
		if (msb)
			putchar('1');
		else
			putchar('0');
		b <<= 1;
	}
}

int main(int argc, char *argv[])
{
	char c, next_line = 0;

	while ((c = getchar()) != EOF) {
		output_binary(c);
		++next_line;
		if (next_line % 2 == 0)
			putchar('\n');
	}

	return 0;
}