-
Notifications
You must be signed in to change notification settings - Fork 84
/
2.c
64 lines (52 loc) · 1.27 KB
/
2.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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define SIZE 5
int btoi(char * st); //二进制字符串转化为整数的函数
char * itob(char * st, int n); //整数转化为二进制字符串的函数
void show_bstr(const char * st); //4位一组显示二进制字符串
int main(int argc, char * argv[])
{
int num1, num2;
char bin_st[CHAR_BIT * sizeof(int) + 1];
if (argc != 3)
{
fprintf(stderr, "The number of arguments is wrong.\n");
exit(EXIT_FAILURE);
}
num1 = btoi(argv[1]);
num2 = btoi(argv[2]);
show_bstr(itob(bin_st, ~num1));
show_bstr(itob(bin_st, ~num2));
show_bstr(itob(bin_st, num1 & num2));
show_bstr(itob(bin_st, num1 | num2));
show_bstr(itob(bin_st, num1 ^ num2));
return 0;
}
int btoi(char * st)
{
int num = 0;
while (*st)
num = (num << 1) + (*st++ - '0');
return num;
}
char * itob(char * st, int n)
{
int i;
const static int size = CHAR_BIT * sizeof(int);
for (i = size - 1; i >= 0; i--, n >>= 1)
st[i] = (01 & n) + '0';
st[size] = '\0';
return st;
}
void show_bstr(const char * st)
{
int i = 0;
while (st[i])
{
putchar(st[i]);
if (++i % 4 == 0 && st[i])
putchar(' ');
}
printf("\n");
}