C - Write program to double integer value by shifting

Requirements

Write program to double integer value by shifting

Hint

use left shift operator.

Demo

#include <stdio.h>

char *to_binary(int n);

int main()/*www .  jav a2s  .  co  m*/
{
    int bshift = 2,x;

    for(x=0;x<8;x++)
    {
        printf("%s %d\n",to_binary(bshift),bshift);
        bshift = bshift << 1;
    }

    return(0);
}

char *to_binary(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);
}

Result

Related Exercise