1
0

Add %b to print() to print integers in binary

This commit is contained in:
Aaron Lindsay 2012-09-21 11:47:45 -04:00
parent 2bd44726e4
commit 583765fd7c
2 changed files with 13 additions and 0 deletions

View File

@ -58,6 +58,16 @@ void putx(unsigned int i) {
}
}
void putb(unsigned int i) {
int j;
puts("0b");
for (j = 0; j < 32; j++) {
putc((i>>(31-j)) & 1 ? '1' : '0');
}
}
int print(char *fmt, ...) {
char *c;
va_list arg;
@ -81,6 +91,9 @@ int print(char *fmt, ...) {
case 'x':
putx(va_arg(arg, unsigned int));
break;
case 'b':
putb(va_arg(arg, unsigned int));
break;
default:
puts("\nError: print(): Invalid formatting character: '");
putc(*c);