-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteger Representations.py
More file actions
204 lines (185 loc) · 7.47 KB
/
Integer Representations.py
File metadata and controls
204 lines (185 loc) · 7.47 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import re
'''
Integer Representations
-Signed Value Representation
-1's Complement Representation
-2's Complement Representation
-Excess Representation
'''
def chopLiteral(binary):
if binary[0] == '0' and binary[1] == 'b':
return binary[2:]#excluding '0b'
elif binary[0] == '-':
return binary[3:]#excluding '-0b'
else:
return binary
#############################################################################################################
#All the 'B-D' functions take in a string of a binary number. Starting with '0b....'
def signValueBD(binary):
binary = chopLiteral(binary)
if binary[0]=='0':
return int(binary,2)
else:
return -1*int(binary[1:],2)#[1:] to discount the sign bit
def excessBD(binary):
binary = chopLiteral(binary)
length = len(binary)
bias = (2**(length-1)) - 1
return int(binary,2) - bias
#e.g: 1101 = -((1-1)*(2**0) + (1-0)*(2**1) + (1-1)*(2**2)) = -2
def onesCompBD(binary):
binary = chopLiteral(binary)
if binary[0]=='0':
return int(binary,2)
else:
inverseBits = ""
for bit in binary[1:]:
inverseBits = inverseBits + (str((1 - int(bit))))#take away each bit from one to invert it
return -1 * int(inverseBits, 2)
def twosCompBD(binary):
binary = chopLiteral(binary)
if binary[0]=='0':
return int(binary,2)
else:
inverseBits = [str(1-int(bit)) for bit in binary[1:]]#makes a list of chars using list comprehension
inverseBits = ''.join(inverseBits)#turns list of chars into a string
print(inverseBits)
return -1 * (int(inverseBits,2) + 1)
#############################################################################################################
def signValueDB(decimal):
if decimal > -1:
usedBits = len(bin(decimal)) - len("0b")
filler0s = byteSize - usedBits
return ('0'*filler0s) + bin(decimal)[2:]
else:
usedBits = len(bin(decimal)) - len("-0b") + len("b")
filler0s = byteSize - usedBits
return "1" + ('0'*filler0s) + bin(decimal)[3:]
def excessDB(decimal):
bias = (2**byteSize)-1
result = decimal + bias
return bin(result)[2:] #remove the '0b' at the start of srting
def onesCompDB(decimal):
if decimal>-1:
usedBits = len(bin(decimal)) - len('0b')
filler0s = byteSize - usedBits
return ('0'*filler0s) + bin(decimal)[2:]
else:
bits = bin(decimal)[3:]
inverseBits = [str(1-int(bit)) for bit in bits]#makes a list of chars using list comprehension
inverseBits = ''.join(inverseBits)#turns list of chars into a string
filler1s = byteSize - len(inverseBits)
return ('1'*filler1s) + inverseBits
def twosCompDB(decimal):
if decimal>-1:
usedBits = len(bin(decimal)) - len('0b')
filler0s = byteSize - usedBits
return ('0'*filler0s) + bin(decimal)[2:]
else:
bits = bin(decimal)[3:]#excluding the '-0b'
inverseBits = [str(1-int(bit)) for bit in bits]#makes a list of chars using list comprehension
inverseBits = ''.join(inverseBits)#turns list of chars into a string
filler1s = byteSize - len(inverseBits)
return bin(int(('1'*filler1s) + inverseBits,2) + 1)[2:]#remove the '0b'
#############################################################################################################
def choice(prompt,inputs):
text = input(prompt)
while text not in inputs:
text = input("please choose from " + str(inputs) + ":\n")
return text
def chooseBinary():
goodInputs = ["binary","bin","b","bi","decimal","dec","d"]
text = input("Convert from Binary OR Decimal?:").lower()
while text not in goodInputs:
print("*INPUT ERROR*")
text = input("Convert from Binary OR Decimal?:").lower()
if text[0]=='b':
return True
else:
return False
#############################################################################################################
def getBinaryInput():
while True:
text = input("Please enter an " + str(byteSize) + " bit binary number: ")
#reg exp for string to be binary = ^[01]+$
if len(text)==byteSize and re.match('^[01]+$',text):
return text
def getIntegerInput():
while True:
text = input("Please enter an integer: ")
if re.match('^\-?[0-9]+$',text):
return int(text)
def getByteLength():
while True:
text = input("Enter the new byte length: ")
if re.match('^[0-9]+$',text):
if int(text) in range(4,17):
return int(text)
#############################################################################################################
def printBD(stdin,result):
print("\nBINARY -> DECIMAL\n" + str(stdin) + " -> " + str(result))
def printDB(stdin,result):
print("\nDECIMAL -> BINARY\n" + str(stdin) + " -> " + str(result))
#############################################################################################################
runProgram = True
byteSize = 8
prompt = ""
prompt += "Program Functions Menu: \n"
prompt += "1. Signed Value Representation\n"
prompt += "2. 1's Complement representation\n"
prompt += "3. 2's Complement Representation\n"
prompt += "4. Excess Representation\n"
prompt += "5. Change Default Byte Length\n"
prompt += "q. Exit Program\n\nChoose from the above options: "
options = ['1','2','3','4','5','q']
while runProgram:
option = choice(prompt,options)
print("\n" + ('#'*80) + "\n")
if option == 'q':
print("Exiting Program..")
runProgram = False
elif option == '1':
print("\t\t\tSIGNED VALUE REPRESENTATION")
if chooseBinary():
stdin = getBinaryInput()
result = signValueBD(stdin)
printBD(stdin,result)
else:
stdin = getIntegerInput()
result = signValueDB(stdin)
printDB(stdin,result)
elif option == '2':
print("\t\t\tONES COMPLEMENT REPRESENTATION")
if chooseBinary():
stdin = getBinaryInput()
result = onesCompBD(stdin)
printBD(stdin,result)
else:
stdin = getIntegerInput()
result = onesCompDB(stdin)
printDB(stdin,result)
elif option == '3':
print("\t\t\tTWOS COMPLEMENT REPRESENTATION")
if chooseBinary():
stdin = getBinaryInput()
result = twosCompBD(stdin)
printBD(stdin,result)
else:
stdin = getIntegerInput()
result = twosCompDB(stdin)
printDB(stdin,result)
elif option == '4':
print("\t\t\tEXCESS REPRESENTATION")
if chooseBinary():
stdin = getBinaryInput()
result = excessBD(stdin)
printBD(stdin,result)
else:
stdin = getIntegerInput()
result = excessDB(stdin)
printDB(stdin,result)
elif option == '5':
print("\t\t\tCHANGE DEFAULT BYTE LENGTH [4-16]")
byteSize = getByteLength()
print("Byte length has been set to " + str(byteSize) + " bytes.")
print("\n" + ('#'*80) + "\n")