Add %b to print() to print integers in binary
This commit is contained in:
parent
2bd44726e4
commit
583765fd7c
@ -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, ...) {
|
int print(char *fmt, ...) {
|
||||||
char *c;
|
char *c;
|
||||||
va_list arg;
|
va_list arg;
|
||||||
@ -81,6 +91,9 @@ int print(char *fmt, ...) {
|
|||||||
case 'x':
|
case 'x':
|
||||||
putx(va_arg(arg, unsigned int));
|
putx(va_arg(arg, unsigned int));
|
||||||
break;
|
break;
|
||||||
|
case 'b':
|
||||||
|
putb(va_arg(arg, unsigned int));
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
puts("\nError: print(): Invalid formatting character: '");
|
puts("\nError: print(): Invalid formatting character: '");
|
||||||
putc(*c);
|
putc(*c);
|
||||||
|
Loading…
Reference in New Issue
Block a user