-
Notifications
You must be signed in to change notification settings - Fork 0
/
C_MM18.cpp
46 lines (44 loc) · 902 Bytes
/
C_MM18.cpp
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
#include <iostream>
using namespace std;
char* toBin(char* bin, int dec) {
int count = 7;
if (dec >= 0) {
while (dec > 0) {
bin[count] = dec % 2 + '0';
dec /= 2;
count--;
}
}
else {
dec = -dec;
while (dec > 0) {
bin[count] = dec % 2 + '0';
dec /= 2;
count--;
}
for (int i = 0; i < 8; i++) {
if (bin[i] == '0') {
bin[i] = '1';
}
else {
bin[i] = '0';
}
}
count = 7;
while (bin[count] != '0') {
bin[count] = '0';
count--;
}
if (count >= 0) {
bin[count] = '1';
}
}
return bin;
}
int main() {
int x;
char bin[] = "00000000";
cin >> x;
cout << toBin(bin, x) << endl;
return 0;
}