Solution for Exercise 17-12
ex1712
#include <stdio.h> char *binbin(unsigned n); int main() { unsigned bshift,x; printf("Type a value 0 to 255: "); scanf("%u",&bshift); for(x=0;x<8;x++) { printf("%s %d\n",binbin(bshift),bshift); bshift >>= 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
Type a value 0 to 255: 128
0000000010000000 128
0000000001000000 64
0000000000100000 32
0000000000010000 16
0000000000001000 8
0000000000000100 4
0000000000000010 2
0000000000000001 1
Copyright © 1997-2024 by QPBC.
All rights reserved