# # Lab 5: Tasks 2 and 3 # def bitwise_or(b1, b2): """ computes and returns the bitwise OR of binary numbers b1 and b2 inputs: b1 and b2 are strings that represent binary numbers """ if b1 == '' and b2 == '': # not strictly needed return '' elif b1 == '': return b2 elif b2 == '': return b1 else: or_rest = bitwise_or(b1[:-1], b2[:-1]) if b1[-1] == '1' or b2[-1] == '1': return or_rest + '1' else: return or_rest + '0' def left_shift(b, n): """ returns the result of performing a left shift of the bitstring b by n places """ if n <= 0: return b else: return b + ('0' * n) def right_shift(b, n): """ returns the result of performing a right shift of the bitstring b by n places """ if n <= 0: return b elif n >= len(b): return '0' else: return b[:-n]