Solution for Exercise 17-13
ex1713
#include <stdio.h> char *binbin(unsigned n); int main() { unsigned b,x; b = 21; for(x=0;x<8;x++) { printf("%s 0x%04X %4d\n",binbin(b),b,b); b<<=1; } return(0); } char *binbin(unsigned n) { static char bin[17]; int x; for(x=0;x<16;x++) { bin[x] = n & 0x8000 ? '1' : '0'; n <<= 1; } bin[x] = '\0'; return(bin); }
Output
0000000000010101 0x0015 21
0000000000101010 0x002A 42
0000000001010100 0x0054 84
0000000010101000 0x00A8 168
0000000101010000 0x0150 336
0000001010100000 0x02A0 672
0000010101000000 0x0540 1344
0000101010000000 0x0A80 2688
Notes
* It's difficult to pluck out the four-bit hexadecimal patterns in the output, but you can do it. Keep Table 17-4 handy in the book as you review the numbers shown above.
* Here is the sample output color-coded to make the groups easier to see:
Copyright © 1997-2024 by QPBC.
All rights reserved