-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
52 lines (37 loc) · 1.13 KB
/
test.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
def binADeci(bin4bits):
decimalValue = 0
base = 1
for i in range(len(bin4bits) - 1, -1, -1):
bitChar = bin4bits[i]
bit = int(bitChar)
decimalValue += bit * base
base *= 2
return decimalValue
def decimalToBinary(decimalValue):
binaryString = ""
if decimalValue == 0:
binaryString = "0"
else:
while decimalValue > 0:
remainder = decimalValue % 2
binaryString = str(remainder) + binaryString
decimalValue = decimalValue // 2
return binaryString
def getUnidades(decimalValue):
digitoUnidades = decimalValue % 10
return digitoUnidades
def getDecenas(decimalValue):
digitoDecenas = (decimalValue // 10) % 10
return digitoDecenas
def decimalToBinary1(decimalValue, bitLength):
binaryString = ""
while bitLength > 0:
remainder = decimalValue % 2
binaryString = str(remainder) + binaryString
decimalValue = decimalValue // 2
bitLength -= 1
return binaryString
print(binADeci('1100'))
print(getUnidades(12))
print(getDecenas(12))
print(decimalToBinary1(1, 4))