MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / logical_left_shift

Function logical_left_shift

bit_manipulation/binary_shifts.py:6–33  ·  view source on GitHub ↗

Take in 2 positive integers. 'number' is the integer to be logically left shifted 'shift_amount' times. i.e. (number << shift_amount) Return the shifted binary representation. >>> logical_left_shift(0, 1) '0b00' >>> logical_left_shift(1, 1) '0b10' >>> logical_le

(number: int, shift_amount: int)

Source from the content-addressed store, hash-verified

4
5
6def logical_left_shift(number: int, shift_amount: int) -> str:
7 """
8 Take in 2 positive integers.
9 'number' is the integer to be logically left shifted 'shift_amount' times.
10 i.e. (number << shift_amount)
11 Return the shifted binary representation.
12
13 >>> logical_left_shift(0, 1)
14 '0b00'
15 >>> logical_left_shift(1, 1)
16 '0b10'
17 >>> logical_left_shift(1, 5)
18 '0b100000'
19 >>> logical_left_shift(17, 2)
20 '0b1000100'
21 >>> logical_left_shift(1983, 4)
22 '0b111101111110000'
23 >>> logical_left_shift(1, -1)
24 Traceback (most recent call last):
25 ...
26 ValueError: both inputs must be positive integers
27 """
28 if number < 0 or shift_amount < 0:
29 raise ValueError("both inputs must be positive integers")
30
31 binary_number = str(bin(number))
32 binary_number += "0" * shift_amount
33 return binary_number
34
35
36def logical_right_shift(number: int, shift_amount: int) -> str:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected