十进制转二进制的算法

方法一:

int main() 
{ 
   int  ival, n = 0; 
   char bin[32];

   scanf("%d", &ival); 
   while(ival > 0) { 
       bin[n++] = ival % 2; 
       ival /= 2; 
   } 
   for(n--; n >= 0; n--) 
       printf("%d", bin[n]); 
   printf("\n");
   getch();
}

 

 

方法二:

int main() 
{ 
int ival, n = 0; 
char bin[32];

scanf("%d", &ival); 
while(ival > 0) { 
bin[n++] = ival & 0x01; 
ival >>= 1; 
} 
for(n--; n >= 0; n--) 
printf("%d", bin[n]); 
printf("\n");
getch();
}

 

posted @ 2013-01-30 10:15  maxspeed  阅读(336)  评论(0编辑  收藏  举报