Skip to content

Commit 52a98aa

Browse files
author
Vivek Patel
committed
Added binary_and_operator.py & binary_xor_operator.py
1 parent cbbc43b commit 52a98aa

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
def binary_and(a: int, b: int):
3+
"""
4+
Take in 2 integers, convert them to binary,
5+
return a binary number that is the
6+
result of a binary and operation on the integers provided.
7+
8+
>>> binary_and(25, 32)
9+
'0b000000'
10+
>>> binary_and(37, 50)
11+
'0b100000'
12+
>>> binary_and(21, 30)
13+
'0b10100'
14+
>>> binary_and(58, 73)
15+
'0b0001000'
16+
>>> binary_and(0, 255)
17+
'0b00000000'
18+
>>> binary_and(256, 256)
19+
'0b100000000'
20+
>>> binary_and(0, -1)
21+
Traceback (most recent call last):
22+
...
23+
ValueError: the value of both input must be positive
24+
>>> binary_and(0, 1.1)
25+
Traceback (most recent call last):
26+
...
27+
TypeError: 'float' object cannot be interpreted as an integer
28+
>>> binary_and("0", "1")
29+
Traceback (most recent call last):
30+
...
31+
TypeError: '<' not supported between instances of 'str' and 'int'
32+
"""
33+
if a < 0 or b < 0:
34+
raise ValueError("the value of both input must be positive")
35+
36+
a_binary = str(bin(a))[2:] # remove the leading "0b"
37+
b_binary = str(bin(b))[2:] # remove the leading "0b"
38+
39+
max_len = max(len(a_binary), len(b_binary))
40+
41+
return "0b" + "".join(
42+
str(int(char_a == "1" and char_b == "1"))
43+
for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len))
44+
)
45+
46+
47+
if __name__ == "__main__":
48+
import doctest
49+
50+
doctest.testmod()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
def binary_xor(a: int, b: int):
3+
"""
4+
Take in 2 integers, convert them to binary,
5+
return a binary number that is the
6+
result of a binary xor operation on the integers provided.
7+
8+
>>> binary_xor(25, 32)
9+
'0b111001'
10+
>>> binary_xor(37, 50)
11+
'0b10111'
12+
>>> binary_xor(21, 30)
13+
'0b1011'
14+
>>> binary_xor(58, 73)
15+
'0b1110011'
16+
>>> binary_xor(0, 255)
17+
'0b11111111'
18+
>>> binary_xor(256, 256)
19+
'0b000000000'
20+
>>> binary_xor(0, -1)
21+
Traceback (most recent call last):
22+
...
23+
ValueError: the value of both input must be positive
24+
>>> binary_xor(0, 1.1)
25+
Traceback (most recent call last):
26+
...
27+
TypeError: 'float' object cannot be interpreted as an integer
28+
>>> binary_xor("0", "1")
29+
Traceback (most recent call last):
30+
...
31+
TypeError: '<' not supported between instances of 'str' and 'int'
32+
"""
33+
if a < 0 or b < 0:
34+
raise ValueError("the value of both input must be positive")
35+
36+
a_binary = str(bin(a))[2:] # remove the leading "0b"
37+
b_binary = str(bin(b))[2:] # remove the leading "0b"
38+
39+
max_len = max(len(a_binary), len(b_binary))
40+
41+
return "0b" + "".join(
42+
str(int(char_a != char_b))
43+
for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len))
44+
)
45+
46+
47+
if __name__ == "__main__":
48+
import doctest
49+
50+
doctest.testmod()

0 commit comments

Comments
 (0)