Solution for Exercise 17-13

ex1713

#include <stdio.h>

char *binbin(int n);

int main()
{
    int 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(int 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);
}

Notes

* Here's sample output:

* 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: