【文檔創建日期:2010-07-02】(我的原創代碼,如有需要引用,請註明本出處。)
1 // Header files included.
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 // Macro definition.
6 #define BIT 8
7 #define SIZE (BIT * sizeof(short int))
8
9 // No.1: Subroutine:
10 short int *Decimal_to_Binary ( short int * const save, unsigned int size, short int num )
11 {
12 unsigned short int i;
13
14 if ( size != (BIT * sizeof(short int)) )
15 {
16 printf ( "The size is not long enough to record the number's all bits./n" );
17 }
18 else
19 {
20 for ( i=0; i<size; i++ )
21 {
22 save[i] = (num & (1 << (size - i - 1))) >> (size - i - 1);
23 }
24 }
25
26 return (save);
27 }
28
對於其他基本數據類型,只需要更換子函數中的sizeof內的類型名和函數參數中save和num的類型即可