-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex1a6.py
166 lines (151 loc) · 4.93 KB
/
ex1a6.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from math import pow,modf
def counter(variable):
start = 0
while start <= len(variable)-1:
yield start;
start = start + 1;
print("Exercicio 1")
r1 = "01110"
for x in counter(r1):
if x == 0:
print("1.)O bit menos significativo é o bit o 0 0\nCovertido para decimal: {}".format(int(r1[x])*pow(2,x)))
elif x == 3:
print("\n1.)O bit mais significativo é o 3\nConvertido para decimal: {}".format(int(r1[x])*pow(2,x)))
r2 = "1010"
r3 = "1100110001"
somar2 = 0
somar3 = 0
for o in counter(r2):
somar2 = somar2 + int(r2[::-1][o])*pow(2,o)
print("\n2.)O número {} em decimal é :{}".format(r2,somar2))
for i in counter(r3):
somar3 = somar3 + int(r3[::-1][i])*pow(2,i)
print("\n3.)O número {} em decimal é :{}".format(r3,somar3))
print("\nExercicio 2\nConverta esse números binário para decimais")
r = [[1,1,0,1,0,1,0,1][::-1], [1,0,1,0,0,0,0,0][::-1],[1,1,0,0,0,1,1][::-1],[1,0,1,0,0,1,1,1][::-1],[1,1,0,1,1,1,1,1][::-1],[1,0,1,1,1,1,0,1][::-1],[0,1,1,1,0,0,0,0][::-1],[1,0,0,0,1,1,1,0][::-1],[0,1,1,0,1,1,1,0][::-1],[1,1,1,1,0,0,0,0][::-1],[1,1,0,0,1,0,0,0][::-1],[1,0,0,0,0,0,1,1][::-1],[0,0,0,0,0,0,0,1][::-1],[1,0,0,0,0,0,1,0][::-1],[1,1,1,0,1,1,1,0][::-1],[1,0,1,0,1,0,1,0][::-1],[0,1,1,0,0,0,1,1][::-1],[0,0,0,1,1,1,0,0][::-1],[0,0,1,1,0,0,0,1][::-1],[1,1,1,0,0,0,1,1][::-1],[1,1,0,1,1,0,0,0][::-1],[1,0,0,1,0,0,0,0][::-1],[0,1,0,1,0,1,0,0][::-1],[1,1,1,0,1,0,1,1][::-1]]
soma = 0
n = 0
for x in r:
soma = 0
for c in counter(x):
soma = soma + x[c]*pow(2,c)
n = n + 1
print("{}){} = {}".format(n,''.join([str(e) for e in x][::-1]),soma))
print("\nExercicio 3")
arrays = [21,552,715]
num = []
n = 0
for unit in arrays:
while unit/2 >= 1:
num.append(int(unit%2))
unit = unit/2
num.append(1)
print("{}.{} = {}".format(n+1,arrays[n] ,''.join([str(c) for c in num][::-1])))
n = n + 1
num = []
print("\nExercicio 4")
exercise = "111.001"
intnumber = []
floatnumber = []
soma0 = 0
soma1 = 0
for x in counter(exercise):
if x > exercise.index("."):
floatnumber.append(float(exercise[x]))
elif x < exercise.index("."):
intnumber.append(float(exercise[x]))
for c in counter(intnumber):
soma0 = soma0 + intnumber[::-1][c] * pow(2, c)
for z in counter(floatnumber):
soma1 = soma1 + floatnumber[z] * pow(2, -(z + 1))
print("O valor binário {} convertido em decimal é:{} ".format(exercise, (soma0 + soma1)))
print("\nExercicio 5")
exercise5 = ["4.8", "3.125"]
exnumber = []
for ex in exercise5:
integerR = modf(float(ex))[1]
floatR = modf(float(ex))[0]
while integerR / 2 >= 1:
exnumber.append(int(integerR % 2))
integerR = integerR / 2
exnumber.append(1)
exnumber.insert(0, ".")
while floatR < 1:
if floatR * 2 < 1:
exnumber.insert(0, "0")
elif floatR * 2 > 1:
exnumber.insert(0, "1")
floatR = floatR * 2
exnumber.insert(0, "1")
strnumber = [str(x) for x in exnumber][::-1]
exnumber = []
print("O número {} em binário é:{} ".format(ex, "".join(strnumber)))
print("\nExercicio 6")
r = "3A7"
result = []
newresult = []
final_result = []
for x in r:
if x == "3":
result.append("0")
result.append("0")
result.append("1")
result.append("1")
elif x == "A":
result.append("1")
result.append("0")
result.append("1")
result.append("0")
elif x == "7":
result.append("0")
result.append("1")
result.append("1")
result.append("1")
for x in result:
newresult.append(x)
if len(newresult) == 3:
if newresult == ["1","1","1"]:
final_result.append("7")
elif newresult == ["1","0","0"]:
final_result.append("4")
elif newresult == ["1","1","0"]:
final_result.append("6")
elif newresult == ["0","0","1"]:
final_result.append("1")
newresult = []
print("1) O número {} em octal é: {}".format(r,''.join(final_result)))
z = ["1100011","001011000000"]
resarray = []
final_array = []
lastarray = []
for c in z:
for v in c:
resarray.append(v)
while (len(resarray)%4) != 0:
resarray.insert(0,"0")
final_array.append(resarray)
resarray = []
for b in final_array:
for char in b:
lastarray.append(char)
if lastarray == [ "0","1","1","0"]:
resarray.append("6")
lastarray = []
elif lastarray == [ "0","0","1","1"]:
resarray.append("3")
lastarray = []
elif lastarray == [ "0","0","0","0"]:
resarray.append("0")
lastarray = []
elif lastarray == ["0","0","1","0"]:
resarray.append("2")
lastarray = []
elif lastarray == ["1","1","0","0"]:
resarray.append("C")
lastarray = []
if resarray == ["6","3"]:
print("2.a)O número {} em hexadecimal é:{}".format(z[0],''.join(resarray)))
resarray = []
elif resarray == ["2","C","0"]:
print("2.b)O número {} em hexadecimal é:{}".format(z[1],''.join(resarray)))
resarray = []