dectobin

#include <stdio.h>

#define L 32

void dectobin(int);

int main()
{
   dectobin(65535);
 
   return 0;
}

void dectobin(int d){
   int i;
   int bin[L];

   for (i = 0; i < L; i++)
      bin[i] = 0;

   i = L-1;
   do{
      printf("\nd = %d", d);
      if ((d%2) == 0)
         bin[i] = 0;
      else
         bin[i] = 1;
      d = d/2;
      i--;
   }while (d != 0);
   
   printf("\n");

   for (i = 0; i < L; i++)
      printf("%d", bin[i]);

   printf("\n");
}

Result:

d = 65535
d = 32767
d = 16383
d = 8191
d = 4095
d = 2047
d = 1023
d = 511
d = 255
d = 127
d = 63
d = 31
d = 15
d = 7
d = 3
d = 1
00000000000000001111111111111111

Advertisements