Solution for Exercise 17-2
ex1702
#include <stdio.h>
char *binbin(unsigned n);
int main()
{
unsigned input;
printf("Type a value 0 to 65535: ");
scanf("%u",&input);
printf("%u is binary %s\n",
input,binbin(input));
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 65535: 65000
65000 is binary 1111110111101000
Notes
* Some things I forgot to tell you in the book:
* Yes, the compiler doesn't complain when you pass the binbin() function an unsigned char instead of an unsigned int, but the typecast truncates the value, so only the left 8 bits are significant.
Copyright © 1997-2026 by QPBC.
All rights reserved
