-
Notifications
You must be signed in to change notification settings - Fork 0
/
booth.cpp
81 lines (73 loc) · 1.46 KB
/
booth.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
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
73
74
75
76
77
78
79
80
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int binario(int m){
vector<int> binario;
while(m/2 > 1){
int c = m%2;
binario.push_back(c);
m = m/2;
}
if(m/2 == 1){
int c = m%2;
binario.push_back(c);
binario.push_back(1);
}
reverse(binario.begin(),binario.end());
int a = binario.size();
for(int i=0; i<a;i++){
cout<< binario[i]<<endl ;
}
}
int complement2(int m){
vector<int> binario;
while(m/2 > 1){
int c = m%2;
binario.push_back(c);
m = m/2;
}
if(m/2 == 1){
int c = m%2;
binario.push_back(c);
binario.push_back(1);
}
reverse(binario.begin(),binario.end());
int a = binario.size();
for(int i=0;i<a;i++){
if (binario[i] == 1)
binario[i] = 0;
else
binario[i] = 1;
}
int sum = 1;
int i = 1;
while(sum == 1){
if(binario[a-i] == 1){
binario[a-i] = 0;
i++;
}
else{
binario[a-i] = 1;
sum = 0;
}
}
for(int i=0;i<a;i++)
cout<<binario[i]<<endl;
}
int main() {
int m,q;
cout<<"Ingrese el multiplicando: " << endl;
cin>> m;
cout<<"Ingrese el multiplicador: " << endl;
cin>> q;
cout<<"El multiplicando " << m << " en binario es :" << endl;
binario(m);
cout<<"El multiplicador " << q << " en binario es :" << endl;
binario(q);
cout<<"El complemento a 2 de " << m << " es :" << endl;
complement2(m);
cout<<"El complemento a 2 de " << q << " es :" << endl;
complement2(q);
return 0;
}