-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary.py
249 lines (208 loc) · 7.88 KB
/
Binary.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#import numpy as np
from tkinter import *
from tkinter import ttk
import customtkinter
# Class that deals with Binary Inputs
class BinaryEntered:
def __init__(self, inputNum):
self.inputNum = inputNum
def signedMagnitude(self):
bitList = []
binaryNum = self.inputNum
for bit in binaryNum:
bitList.append(bit)
if bitList[0] == "1":
bitList.pop(0)
newBin = ''.join(bitList)
binaryToInt = int(newBin, 2)
binaryToInt = -abs(binaryToInt)
else:
binaryToInt = int(binaryNum, 2)
return "Signed Magnitude: " + str(binaryToInt)
def onesComplement(self):
binaryInt = self.inputNum
bit_list = []
for bit in binaryInt:
if bit == "0":
bit = "1"
elif bit == "1":
bit = "0"
bit_list.append(bit)
onesComp = ''.join(bit_list)
onesCompInt = int(onesComp, 2)
if bit_list[0] == "0":
onesCompInt = -abs(onesCompInt)
return "Ones Complement: " + str(onesCompInt)
def twosComplement(self):
compliment = ""
bit_list = []
for bit in self.inputNum:
bit_list.append(bit)
for i in range(len(self.inputNum)):
if self.inputNum[i] == '0':
compliment = compliment + '1'
else:
compliment = compliment + '0'
inputBit = compliment
carry = 1
compliment = ""
compInt = 0
for i in range(len(inputBit) - 1, -1, -1):
if inputBit[i] == '0':
if carry == 1:
compliment = '1' + compliment
carry = 0
else:
compliment = inputBit[i] + compliment
elif inputBit[i] == '1':
if carry == 1:
compliment = '0' + compliment
else:
compliment = inputBit[i] + compliment
else:
compliment = inputBit[i] + compliment
compInt = int(compliment, 2)
if bit_list[0] == "1":
compInt = -abs(compInt)
return "Twos Complement: " + str(compInt)
def excess128Notation(self):
intVal = int(self.inputNum, 2)
excessValue = 0
if intVal < 128:
excessValue = intVal + 128
elif intVal > 128:
excessValue = intVal - 128
return "Excess 128 Notation: " + str(excessValue)
# Class that deals with Integer inputs
class IntEntered:
def __init__(self, inputNum):
self.inputNum = inputNum
def signedMagnitude(self):
a = format(int(self.inputNum), '08b')
bit_list = []
for bit in a:
bit_list.append(bit)
if int(self.inputNum) < 0:
bit_list[0] = '1'
else:
bit_list[0] = '0'
signedNum = ''.join(bit_list)
return "Signed Magnitude: " + signedNum
def binaryInt(self):
a = format(self.inputNum, '08b')
return a
def onesComplement(self):
a = format(int(self.inputNum), '08b')
bit_list = []
for bit in a:
if bit == '0':
bit = '1'
elif bit == '1':
bit = '0'
bit_list.append(bit)
if int(self.inputNum) < 0:
bit_list[0] = '1'
elif int(self.inputNum) > 0:
bit_list[0] = '0'
onesComp = ''.join(bit_list)
return "Ones Complement: " + str(onesComp)
def twosComplement(self):
return "Twos Complement: " + str(np.binary_repr(int(self.inputNum)))
def excess128Notation(self):
intVal = int(self.inputNum)
excessValue = 0
if intVal < 128:
excessValue = intVal + 128
elif intVal > 128:
excessValue = intVal - 128
excessBinaryValue = format(excessValue, '08b')
return "Excess-128 Notation: " + excessBinaryValue
run = False
# while loop that runs all the classes and performs a check on the binary input
while run:
BinOrInt = input("Press 1 to enter a Binary number and 2 to enter an Integer: ")
innerRun = True
while innerRun:
if BinOrInt == "1":
inputBin = input("Enter a binary number: ")
try:
binaryEntered = BinaryEntered(inputBin)
print(binaryEntered.signedMagnitude())
print(binaryEntered.onesComplement())
print(binaryEntered.twosComplement())
print(binaryEntered.excess128Notation())
except ValueError:
print("Invalid Input a Binary Number Consists of 1's and 0's")
continue
if BinOrInt == "2":
inputInt = input("Enter an Integer: ")
intEntered = IntEntered(inputInt)
print(intEntered.signedMagnitude())
print(intEntered.onesComplement())
print(intEntered.twosComplement())
print(intEntered.excess128Notation())
innerRun = False
runAgain = input("Do you want to run it again (Y/N)? ")
if runAgain.lower() == "n":
run = False
#GUI for the program
root = Tk()
root.title("Binary Converter")
root.geometry("800x400")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky="NSEW")
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
inputNum = StringVar()
# outputs
signedMag = StringVar()
onesComp = StringVar()
twosComp = StringVar()
excess128 = StringVar()
BinOrInt = IntVar()
errorMsg = StringVar()
#checkbox to select binary or integer
ttk.Checkbutton(mainframe, text="Binary", variable=BinOrInt).grid(column=3, row=1, sticky="NSEW")
ttk.Label(mainframe).grid(column=3, row=0, sticky="NSEW")
#textbox to display an error message
ttk.Label(mainframe, textvariable=errorMsg).grid(column=2, row=0, sticky="NSEW")
# Function that converts the binary input to the different types
def convert():
try:
inputValue = inputNum.get()
if inputValue == "":
pass
else:
if BinOrInt.get() == 1:
binaryEntered = BinaryEntered(inputValue)
signedMag.set(binaryEntered.signedMagnitude())
onesComp.set(binaryEntered.onesComplement())
twosComp.set(binaryEntered.twosComplement())
excess128.set(binaryEntered.excess128Notation())
else:
intEntered = IntEntered(inputValue)
signedMag.set(intEntered.signedMagnitude())
onesComp.set(intEntered.onesComplement())
twosComp.set(intEntered.twosComplement())
excess128.set(intEntered.excess128Notation())
except ValueError:
# Error message if the input is not a binary or integer
errorMsg.set("Invalid Input a Binary Number Consists of 1's and 0's")
#display the error message for 5 seconds
root.after(5000, lambda: errorMsg.set(""))
return
# Labels and buttons for the GUI
ttk.Label(mainframe, text="Enter a Number").grid(column=2, row=1, sticky="NSEW")
ttk.Label(mainframe, text="Number: ").grid(column=1, row=2, sticky="NSEW")
inputNumEntry = ttk.Entry(mainframe, width=7, textvariable=inputNum)
inputNumEntry.grid(column=2, row=2, sticky="NSEW")
ttk.Label(mainframe).grid(column=1, row=3, sticky=E)
ttk.Label(mainframe, textvariable=signedMag).grid(column=2, row=3, sticky="NSEW")
ttk.Label(mainframe).grid(column=1, row=4, sticky=E)
ttk.Label(mainframe, textvariable=onesComp).grid(column=2, row=4, sticky="NSEW")
ttk.Label(mainframe).grid(column=1, row=5, sticky=E)
ttk.Label(mainframe, textvariable=twosComp).grid(column=2, row=5, sticky="NSEW")
ttk.Label(mainframe).grid(column=1, row=6, sticky=E)
ttk.Label(mainframe, textvariable=excess128).grid(column=2, row=6, sticky="NSEW")
ttk.Button(mainframe, text="Convert", command=convert).grid(column=3, row=2, sticky="NSEW")
root.mainloop()