Question: Write a function called bitwise_and(b1, b2) that takes as inputs two strings b1 and b2that represent binary numbers. The function should use recursion to compute
Write a function called bitwise_and(b1, b2) that takes as inputs two strings b1 and b2that represent binary numbers. The function should use recursion to compute the bitwise AND of the two numbers and return the result in the form of a string. For example:
>>> bitwise_and('11010', '10011') '10010' >>> bitwise_and('1001111', '11011') '0001011' Notes/hints:
Base cases: Because the function will be called recursively on smaller and smaller strings, you will eventually get down to an empty string for one or both of the inputs.
If both inputs are the empty string, the function should return the empty string.
If only one of the inputs is the empty string, the function should return a string that represents the result of ANDing the other input with 0s.
For example:
>>> bitwise_and('', '') '' >>> bitwise_and('101', '') '000' >>> bitwise_and('', '11010') '00000' Hint: Use string multiplication to obtain a string with the appropriate number of 0s.
You should use recursion to AND the rest of the bits in the two numbers. When you do so, make sure that you end up considering the bits from right to left, rather than from left to right.
As usual, we recommend making the recursive call at the start of the recursive case and storing its return value in a variable. Then you can use that variable to construct the value that you will return.
You will need to use conditional execution (if-else or if-elif-else) to decide what to return, based on the bits that are being ANDed by the current call of the function. Use concrete cases as needed to figure out the logic.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
