-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbinary.c
72 lines (66 loc) · 1.52 KB
/
binary.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <math.h>
void decimalToBinary(int num);
void binaryToDecimal(long num);
void decimal2Binary(int num);
int main()
{
decimalToBinary(20);
decimal2Binary(20);
binaryToDecimal(10100L);
return 0;
}
int length(double n)
{
return (int)log10(n) + 1;
}
void decimalToBinary(int num)
{
int remainder;
int pvoit = 2;
// 不断除2取余,倒序打印余数。
printf("\n %d convert to binary: ", num);
while (num != 0)
{
remainder = num % pvoit;
num /= pvoit;
// printf("%d / %d = %d remainder %d", num, pvoit, remainder);
printf("%d", remainder);
}
}
void decimal2Binary(int num)
{
long binary = 0L;
int remainder;
int i = 1;
int step = 1;
int pvoit = 2;
int origin = num;
while (num != 0)
{
remainder = num % pvoit;
// printf("Step %d: %d/2, remainder = %d, quotient = %d\n", step++, num, remainder, num / 2);
num /= pvoit;
// 以余数乘以位数相加
binary += remainder * 10;
i *= 10;
}
printf("\n %d convert to binary: %ld", origin, binary);
}
void binaryToDecimal(long num)
{
long origin = num;
int decimal = 0;
int i = 0;
int remainder;
int pivot = 10;
while (num != 0)
{
remainder = num % pivot;
num /= pivot;
// 从右往左用二进制的每个数去乘以2的次方,次方是数字的位置,以0开始,从右往左递增
decimal += remainder * pow(2, i);
i++;
}
printf("\n %ld convert to decimal: %d", origin, decimal);
}