Skip to content

Commit 4a3b8d6

Browse files
author
Vivek
authored
Added binary_xor_operator.py and binary_and_operator.py (TheAlgorithms#2433)
* Added binary_and_operator.py & binary_xor_operator.py * Updated binary_and_operator.py * Updated binary_xor_operator.py * Updated binary_xor_operator.py
1 parent 5f9be0a commit 4a3b8d6

File tree

2 files changed

+102
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)