forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_shifts.py
98 lines (83 loc) · 2.48 KB
/
binary_shifts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from binary_twos_complement import twos_complement
def logical_left_shift(a: int, b: int) -> str:
"""
Take in 2 positive integers.
'a' is the integer to be logically left shifted 'b' times.
i.e. (a << b)
Return the shifted binary representation.
>>> logical_left_shift(0, 1)
'0b00'
>>> logical_left_shift(1, 1)
'0b10'
>>> logical_left_shift(1, 5)
'0b100000'
>>> logical_left_shift(17, 2)
'0b1000100'
>>> logical_left_shift(1983, 4)
'0b111101111110000'
>>> logical_left_shift(1, -1)
Traceback (most recent call last):
...
ValueError: both inputs must be positive integers
"""
if a < 0 or b < 0:
raise ValueError("both inputs must be positive integers")
binary_a = str(bin(a))
binary_a += '0' * b
return binary_a
def logical_right_shift(a: int, b: int) -> str:
"""
Take in positive 2 integers.
'a' is the integer to be logically right shifted 'b' times.
i.e. (a >> b)
Return the shifted binary representation.
>>> logical_right_shift(0, 1)
'0b0'
>>> logical_right_shift(1, 1)
'0b0'
>>> logical_right_shift(1, 5)
'0b0'
>>> logical_right_shift(17, 2)
'0b100'
>>> logical_right_shift(1983, 4)
'0b1111011'
>>> logical_right_shift(1, -1)
Traceback (most recent call last):
...
ValueError: both inputs must be positive integers
"""
if a < 0 or b < 0:
raise ValueError("both inputs must be positive integers")
binary_a = str(bin(a))[2:]
if b >= len(binary_a):
return '0b0'
shifted_binary_a = binary_a[:len(binary_a) - b]
return '0b' + shifted_binary_a
def arithmetic_right_shift(a: int, b: int) -> str:
"""
Take in 2 integers.
'a' is the integer to be arithmetically right shifted 'b' times.
Return the shifted binary representation.
>>> arithmetic_right_shift(0, 1)
'0b00'
>>> arithmetic_right_shift(1, 1)
'0b00'
>>> arithmetic_right_shift(-1, 1)
'0b11'
>>> arithmetic_right_shift(17, 2)
'0b000100'
>>> arithmetic_right_shift(-17, 2)
'0b111011'
>>> arithmetic_right_shift(-1983, 4)
'0b111110000100'
"""
if a >= 0:
binary_a = '0' + str(bin(a)).strip('-')[2:]
else:
binary_a = twos_complement(a)[2:]
if b >= len(binary_a):
return '0b' + binary_a[0] * len(binary_a)
return '0b' + binary_a[0] * b + binary_a[:len(binary_a) - b]
if __name__ == "__main__":
import doctest
doctest.testmod()