2014年9月23日 星期二

將數字轉成二進制顯示


#include <stdio.h>
#include <stdlib.h>

const char *byte_to_binary(unsigned int x)
{

 int i =0;
 char b[50]={'\0'};
 
 for(i = 0 ; i < sizeof(unsigned int)*8;i++){
        if( 0 != i && 0 == i %4){
   strcat(b , " ");
  }  
  strcat(b , (x & (1 << (31 - i))) ? "1" : "0");
 }
 
    return b;
}

int main(void)
{
 printf("%s\n",show_binary(13));
 return 0;
}

沒有留言:

張貼留言