Take in positive 2 integers. 'number' is the integer to be logically right shifted 'shift_amount' times. i.e. (number >>> shift_amount) Return the shifted binary representation. >>> logical_right_shift(0, 1) '0b0' >>> logical_right_shift(1, 1) '0b0' >>> logical_
(number: int, shift_amount: int)
| 34 | |
| 35 | |
| 36 | def logical_right_shift(number: int, shift_amount: int) -> str: |
| 37 | """ |
| 38 | Take in positive 2 integers. |
| 39 | 'number' is the integer to be logically right shifted 'shift_amount' times. |
| 40 | i.e. (number >>> shift_amount) |
| 41 | Return the shifted binary representation. |
| 42 | |
| 43 | >>> logical_right_shift(0, 1) |
| 44 | '0b0' |
| 45 | >>> logical_right_shift(1, 1) |
| 46 | '0b0' |
| 47 | >>> logical_right_shift(1, 5) |
| 48 | '0b0' |
| 49 | >>> logical_right_shift(17, 2) |
| 50 | '0b100' |
| 51 | >>> logical_right_shift(1983, 4) |
| 52 | '0b1111011' |
| 53 | >>> logical_right_shift(1, -1) |
| 54 | Traceback (most recent call last): |
| 55 | ... |
| 56 | ValueError: both inputs must be positive integers |
| 57 | """ |
| 58 | if number < 0 or shift_amount < 0: |
| 59 | raise ValueError("both inputs must be positive integers") |
| 60 | |
| 61 | binary_number = str(bin(number))[2:] |
| 62 | if shift_amount >= len(binary_number): |
| 63 | return "0b0" |
| 64 | shifted_binary_number = binary_number[: len(binary_number) - shift_amount] |
| 65 | return "0b" + shifted_binary_number |
| 66 | |
| 67 | |
| 68 | def arithmetic_right_shift(number: int, shift_amount: int) -> str: |
nothing calls this directly
no outgoing calls
no test coverage detected