-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarysystem.py
More file actions
225 lines (183 loc) · 7.75 KB
/
binarysystem.py
File metadata and controls
225 lines (183 loc) · 7.75 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//Email: rouamohamedse@gmail.com
def binary_addition():
first_number = input("Input the first number: ")
second_number = input("Input the second number: ")
# Find the maximum length to align numbers
if len(first_number) >= len(second_number):
max_range = len(first_number)
else:
max_range = len(second_number)
"""##### 01101 + 10111 = 100100 ==> overflow #####"""
# Check if each digit is a valid binary digit (0 or 1)
for i in range(max_range - 1, -1, -1):
if first_number[i] != '1' and first_number[i] != '0' or second_number[i] != '1' and second_number[i] != '0':
print('Invalid input, please enter a valid binary number')
return False
len_first = len(first_number)
len_second = len(second_number)
# Make both numbers of equal length by adding leading zeros
while len(first_number) > len(second_number):
second_number = "0" + second_number
while len(second_number) > len(first_number):
first_number = "0" + first_number
result = ''
carry = 0
# Perform binary addition
for i in range(max_range - 1, -1, -1):
if int(first_number[i]) + int(second_number[i]) + carry == 0:
result = '0' + result
carry = 0
elif int(first_number[i]) + int(second_number[i]) + carry == 1:
result = '1' + result
carry = 0
elif int(first_number[i]) + int(second_number[i]) + carry == 2:
result = '0' + result
carry = 1
elif int(first_number[i]) + int(second_number[i]) + carry == 3:
result = '1' + result
carry = 1
# If there is a carry after the loop, add it
if carry == 1:
result = "1" + result
# Print the result of binary addition
print(f"{first_number} + {second_number} = {result}")
# Function for binary subtraction
def binary_subtraction():
first_number = input("Input the first number: ")
second_number = input("Input the second number: ")
# Determine the maximum length of the binary numbers
if len(first_number) >= len(second_number):
max_range = len(first_number)
else:
max_range = len(second_number)
for i in range(max_range - 1, -1, -1):
if first_number[i] != '1' and first_number[i] != '0' or second_number[i] != '1' and second_number[i] != '0':
print('Invalid input, please enter a valid binary number')
return False
# Store the original lengths for later use
len_first = len(first_number)
len_second = len(second_number)
# Make the binary numbers of equal length by padding with zeros
while len(first_number) > len(second_number):
second_number = "0" + second_number
while len(second_number) > len(first_number):
first_number = "0" + first_number
result = ''
borrow = 0
# Iterate over each bit from right to left
"""##### 01001 - 00111 = 00010 #####"""
for i in range(max_range - 1, -1, -1):
# Get the current bits of the binary numbers
bit1 = int(first_number[i])
bit2 = int(second_number[i])
# Calculate the difference between the bits and consider the borrow
difference = bit1 - bit2 - borrow
# If the difference is negative, adjust and set borrow to 1
if difference < 0:
difference += 2
borrow = 1
else:
borrow = 0
# Append the current bit of the result to the left
result = str(difference) + result
# If there's a remaining borrow, append it to the left
if borrow == 1:
result = '1' + result
# Print the result of binary subtraction
print(f"{first_number} - {second_number} = {result}")
# Function to calculate the first complement of a binary number
def first_complement():
num = input("Enter a binary number: ")
my_list = []
"""##### 1110 ==>> 0001 #####"""
# Check if each digit is a valid binary digit (0 or 1)
for i in range(len(num)):
if num[i] != '1' and num[i] != '0':
print('Invalid input, please enter a valid binary number')
return False
# If the user input a binary number, compute its complement
my_list.append('1' if num[i] == '0' else '0')
# Print the result of the first complement
print('1s complement:', ''.join(my_list))
# Return the result as a string
return ''.join(my_list)
# Function to calculate the second complement
def second_complement():
# Calculate the first complement
first_number = first_complement()
"""##### 000011 ===>> 111101 #####"""
# Initialize the second number for binary addition
second_number = "1"
# Ensure both numbers are of equal length by adding leading zeros
while len(first_number) > len(second_number):
second_number = "0" + second_number
while len(second_number) > len(first_number):
first_number = "0" + first_number
result = ''
carry = 0
max_range = len(first_number)
# Perform binary addition (which is equivalent to the second complement)
for i in range(max_range - 1, -1, -1):
if int(first_number[i]) + int(second_number[i]) + carry == 0:
result = '0' + result
carry = 0
elif int(first_number[i]) + int(second_number[i]) + carry == 1:
result = '1' + result
carry = 0
elif int(first_number[i]) + int(second_number[i]) + carry == 2:
result = '0' + result
carry = 1
elif int(first_number[i]) + int(second_number[i]) + carry == 3:
result = '1' + result
carry = 1
# If there is a carry after the loop, add it
if carry == 1:
result = "1" + result
# Print the result of the second complement
print("Second complement:", result)
# Main binary calculator function
def binary_calculator():
while True:
# Display main menu
print("** Binary Calculator **")
print("A) Insert new numbers")
print("B) Exit")
# Get user's choice for main menu
choice1 = input("Enter your choice: ").lower()
# Check the user's choice for main menu
if choice1 == 'a':
# Display submenu for operations
print("** Please select the operation **")
print("A) Compute one's complement")
print("B) Compute two's complement")
print("C) Addition")
print("D) Subtraction")
# Get user's choice for the specific operation
choice2 = input("Enter your choice: ").lower()
# Check the user's choice for the specific operation
if choice2 == 'a':
# Compute and print the first complement
result = first_complement()
elif choice2 == 'b':
# Compute and print the second complement
second_complement()
elif choice2 == 'c':
# Perform and print binary addition
binary_addition()
elif choice2 == 'd':
# Perform and print binary subtraction
binary_subtraction()
else:
# Invalid choice for the specific operation
print("Please select a valid choice.")
continue
elif choice1 == 'b':
# Exit the program
print("Exiting program. Goodbye!")
break
else:
# Invalid choice for main menu
print("Please select a valid choice.")
continue
# Call the main binary calculator function
binary_calculator()