include iostream include cstring using namespace std class Binary prot

 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
#include <iostream>
#include <cstring>
using namespace std;
class Binary {
protected:
int data[32];
int a;
public:
Binary(){
for (int i = 0; i < 32; ++i)
data[i] = 0;
}
Binary(int value){
for (int i = 0; i < 32; ++i)
data[i] = 0;
int t = 31;
while (value) {
data[t--] = value % 2;
value /= 2;
}
}
Binary(char *s){
int size = strlen(s)-1;
for (int i = 0; i < 32; ++i)
data[i] = 0;
for (int i = 31; size >= 0; --size)
data[i--] = s[size] - '0';
}
void show(){
int i = 0;
while (i < 32 && !data[i]) ++i;
if (i == 32) {
cout << 0;
return;
}
for (; i < 32; ++i)
cout << data[i];
}
int to_int(){
int pow = 1, res = 0;
for (int i = 31; i >= 0; --i) {
res += data[i] * pow;
pow *= 2;
}
return res;
}
Binary operator+(Binary other){
return Binary(to_int(a) + to_int(other.a));
}
};
int main(){
Binary x , y;
}