-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathbinary_and_operator.py
More file actions
51 lines (40 loc) · 1.2 KB
/
binary_and_operator.py
File metadata and controls
51 lines (40 loc) · 1.2 KB
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
"""Bitwise AND helper.
Return a zero-padded binary string representing ``a & b`` where the width is the
maximum bit length of the inputs. Only non-negative integers are accepted.
>>> binary_and(25, 32)
'0b000000'
>>> binary_and(37, 50)
'0b100000'
>>> binary_and(21, 30)
'0b10100'
>>> binary_and(58, 73)
'0b0001000'
>>> binary_and(0, 255)
'0b00000000'
>>> binary_and(256, 256)
'0b100000000'
Invalid inputs raise clear exceptions:
>>> binary_and(0, -1)
Traceback (most recent call last):
...
ValueError: inputs must be non-negative integers
>>> binary_and(0, 1.1)
Traceback (most recent call last):
...
TypeError: inputs must be integers
>>> binary_and("0", "1")
Traceback (most recent call last):
...
TypeError: inputs must be integers
"""
# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm
def binary_and(a: int, b: int) -> str:
if not isinstance(a, int) or not isinstance(b, int):
raise TypeError("inputs must be integers")
if a < 0 or b < 0:
raise ValueError("inputs must be non-negative integers")
max_len = max(a.bit_length(), b.bit_length())
return f"0b{(a & b):0{max_len}b}"
if __name__ == "__main__":
import doctest
doctest.testmod()