Skip to content

Commit 66ed3b4

Browse files
authored
Update binary_and_operator.py
1 parent 46e35f3 commit 66ed3b4

File tree

1 file changed

+1
-41
lines changed

1 file changed

+1
-41
lines changed

bit_manipulation/binary_and_operator.py

+1-41
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,7 @@
11
# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm
22

33

4-
def binary_and(a: int, b: int) -> str:
5-
"""
6-
Take in 2 integers, convert them to binary,
7-
return a binary number that is the
8-
result of a binary and operation on the integers provided.
9-
10-
>>> binary_and(25, 32)
11-
'0b000000'
12-
>>> binary_and(37, 50)
13-
'0b100000'
14-
>>> binary_and(21, 30)
15-
'0b10100'
16-
>>> binary_and(58, 73)
17-
'0b0001000'
18-
>>> binary_and(0, 255)
19-
'0b00000000'
20-
>>> binary_and(256, 256)
21-
'0b100000000'
22-
>>> binary_and(0, -1)
23-
Traceback (most recent call last):
24-
...
25-
ValueError: the value of both inputs must be positive
26-
>>> binary_and(0, 1.1)
27-
Traceback (most recent call last):
28-
...
29-
ValueError: Unknown format code 'b' for object of type 'float'
30-
>>> binary_and("0", "1")
31-
Traceback (most recent call last):
32-
...
33-
TypeError: '<' not supported between instances of 'str' and 'int'
34-
"""
35-
if a < 0 or b < 0:
36-
raise ValueError("the value of both inputs must be positive")
37-
38-
a_binary = format(a, "b")
39-
b_binary = format(b, "b")
40-
41-
max_len = max(len(a_binary), len(b_binary))
42-
43-
return "0b" + "".join(
44-
str(int(char_a == "1" and char_b == "1"))
4+
== "1" and char_b == "1"))
455
for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len))
466
)
477

0 commit comments

Comments
 (0)