Question

In Python: This will require you to write several functions, and then use them in a...

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.

Homework Answers

Answer #1

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"))
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
solve in python 3.8 please The question is about Strings Modify your function so that it...
solve in python 3.8 please The question is about Strings Modify your function so that it can handle the following two situations: The string given as an argument has no characters. In this case, you should return the empty string. The string given as an argument has one character. In this case, you should return the one-character string given to you. Without any consideration for these cases, your function probably causes an error or returns the wrong result in the...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
Programming Challenge B write a simple program that takes as input 2 16-bit strings of 0's...
Programming Challenge B write a simple program that takes as input 2 16-bit strings of 0's and 1's. (For example one string is assigned X and on string is assigned Y). Compute and and display the bitwise AND, bitwise OR, and bitwise XOR of the two string, (For example, the resultant string is Z). You may use the programming language of your choice. Alternatively, if you do not know a programming language, you may describe your algorithm in plain text...
Write a template function maxn() that takes as its arguments an array of items of type...
Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the array. The number of elements should take the default value of 10. The program should include a specialization that takes an array of strings as an argument and returns the longest string. (If there is a tie, the function should return the first one...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
1. Please write the following in C++ also please show all output code and comment on...
1. Please write the following in C++ also please show all output code and comment on code. 2. Also, use CPPUnitLite to write all test and show outputs for each test. Write CppUnitLite tests to verify correct behavior for all the exercises. The modifications are aimed at making the exercises more conducive to unit tests. Write a function that swaps (exchanges the values of two integers). Use int* as the argument type. Write a second swap function using a reference...
Write a function called char_counter that counts the number of a certain character in a text...
Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...
This is an intro to Python question: #Write a function called linear() that takes two parameters...
This is an intro to Python question: #Write a function called linear() that takes two parameters #- a list of strings and a string. Write this function so #that it returns the first index at which the string is #found within the list if the string is found, or False if #it is not found. You do not need to worry about searching #for the search string inside the individual strings within #the list: for example, linear(["bobby", "fred"], "bob") #should...
Write a Python function first_chars that takes one parameter, which is a nested list of strings....
Write a Python function first_chars that takes one parameter, which is a nested list of strings. It returns a string containing the first character of all of the non-empty strings (at whatever depth they occur in the nested list) concatenated together. Here are a couple of examples of how the function should behave when you're done: >>> first_chars(['Boo', 'is', 'happy', 'today']) 'Biht' >>>first_chars(['boo', 'was', ['sleeping', 'deeply'], 'that', 'night', ['as', ['usual']]]) 'bwsdtnau'
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT