In Python: This will require you to write several functions, and then use them in a program. Logical Calculator The logical calculator does math, but with logical operators. In logic, we represent a bit with 0 as false and a bit with 1 as true. The logical operators are NOT, AND and OR. Bitwise logical calculations operate on each bit of the input. The NOT operator works on just one three-bit argument. NOT 011 = 100 The AND operator works on two three-bit arguments: 011 AND 010 = 010 The OR operator also works on two three-bit arguments: 011 OR 010 = 011 Note that the above are examples to illustrate what a logical calculator does. It is not supposed to be the output of your program. The actual output is below. As you can see, the calculator works bitwise. That is, for an operation that requires two arguments, the first character (bit) of the first string is operated on with the first character (bit) of the second string, and the second character (bit) of the first string is operated on with the second character (bit) of the second string, and so on.
Task 1 Write a function one_bit_NOT that takes one argument, a character that is either '0' or '1'. It should perform the NOT operation and return a string with a single character as the result. I.e., if the character argument is "0", it returns a "1"'. If the chararacter argument is "1", it returns a "0".
Task 2 Write a function three_bit_NOT that takes one argument, a string that has three characters of either '0' or '1', and uses the one_bit_NOT function to calculate the result of applying bitwise logical NOT to the string. It returns a string as a result. For example, three_bit_NOT applied to the string argument "000" should return "111".
Task 3 Write a function one_bit_OR that takes two arguments, a and b, which are strings of single characters of either "0" or "1". It then performs the OR operation on the two chars (a OR b) and returns a string with a single character as the result.
Task 4 Write a function three_bit_OR that takes two arguments. The arguments are two strings that have exactly three characters of either "0"' or "1"', and uses the one_bit_OR function to calculate the result of applying bitwise logical OR to the string. It returns a string as a result.
Task 5 Write a function one_bit_AND that takes two arguments, a and b, which are strings of single characters of either "0" or "1". It then performs the AND operation on the two characters (a AND b) and returns a string with a single character as the result.
Task 6 Write a function three_bit_AND that takes two arguments. The arguments are two strings that have exactly three characters of either 0 or 1, and uses the one_bit_AND function to calculate the result of applying bitwise logical AND to the string. It returns a string as a result.
Hey Bud, here's your code. Feel free to ask any doubt. Consider giving an upvote :) Thankyou
#!/usr/bin/python3
def one_bit_NOT(bit):
return str(1^int(bit))
def one_bit_OR(x , y):
return str(int(x) | int(y))
def one_bit_AND(x , y):
return str(int(x) & int(y))
def three_bit_OR(x , y):
final = ""
for i in range(0,len(x)):
final += one_bit_OR(x[i] , y[i])
return final
def three_bit_AND(x , y):
final = ""
for i in range(0,len(x)):
final += one_bit_AND(x[i] , y[i])
return final
def three_bit_NOT(s):
final = ""
for i in range(0,len(s)):
final += one_bit_NOT(s[i])
return final
## Tests
print(three_bit_OR("111","001"))
print(three_bit_AND("111","001"))
print(three_bit_NOT("111"))
Get Answers For Free
Most questions answered within 1 hours.