Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #define size 10//how many bits to present the integer
  5.  
  6. void getBits(int,int[],int);
  7.  
  8. int main(void){
  9.     int bits [size]={0};//initial all bits to 0;
  10.     getBits(24,bits,8);//10進位24的8進位表示法
  11.     for(int i=size-1;i>=0;i--){
  12.             printf("%d",bits[i]);
  13.     }
  14.     return 0;
  15. }
  16.  
  17. void getBits(int integer,int bits[],int b){
  18.     int q=integer;
  19.     int k=0;
  20.     while(q!=0){
  21.         bits[k]=q%b;//get the mod;
  22.         q=ceil(q/b);//Returns the smallest integer value greater than or equal to x.
  23.         k+=1;
  24.     }
  25. }