From 52a98aa030a623b2a791b8516ad3225bbfc4e851 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Tue, 15 Sep 2020 20:27:57 +0530 Subject: [PATCH 1/4] Added binary_and_operator.py & binary_xor_operator.py --- bit_manipulation/binary_and_operator.py | 50 +++++++++++++++++++++++++ bit_manipulation/binary_xor_operator.py | 50 +++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 bit_manipulation/binary_and_operator.py create mode 100644 bit_manipulation/binary_xor_operator.py diff --git a/bit_manipulation/binary_and_operator.py b/bit_manipulation/binary_and_operator.py new file mode 100644 index 000000000000..15c7900c6a48 --- /dev/null +++ b/bit_manipulation/binary_and_operator.py @@ -0,0 +1,50 @@ + +def binary_and(a: int, b: int): + """ + Take in 2 integers, convert them to binary, + return a binary number that is the + result of a binary and operation on the integers provided. + + >>> 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' + >>> binary_and(0, -1) + Traceback (most recent call last): + ... + ValueError: the value of both input must be positive + >>> binary_and(0, 1.1) + Traceback (most recent call last): + ... + TypeError: 'float' object cannot be interpreted as an integer + >>> binary_and("0", "1") + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' + """ + if a < 0 or b < 0: + raise ValueError("the value of both input must be positive") + + a_binary = str(bin(a))[2:] # remove the leading "0b" + b_binary = str(bin(b))[2:] # remove the leading "0b" + + max_len = max(len(a_binary), len(b_binary)) + + return "0b" + "".join( + str(int(char_a == "1" and char_b == "1")) + for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len)) + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/bit_manipulation/binary_xor_operator.py b/bit_manipulation/binary_xor_operator.py new file mode 100644 index 000000000000..9f3a4063d4d9 --- /dev/null +++ b/bit_manipulation/binary_xor_operator.py @@ -0,0 +1,50 @@ + +def binary_xor(a: int, b: int): + """ + Take in 2 integers, convert them to binary, + return a binary number that is the + result of a binary xor operation on the integers provided. + + >>> binary_xor(25, 32) + '0b111001' + >>> binary_xor(37, 50) + '0b10111' + >>> binary_xor(21, 30) + '0b1011' + >>> binary_xor(58, 73) + '0b1110011' + >>> binary_xor(0, 255) + '0b11111111' + >>> binary_xor(256, 256) + '0b000000000' + >>> binary_xor(0, -1) + Traceback (most recent call last): + ... + ValueError: the value of both input must be positive + >>> binary_xor(0, 1.1) + Traceback (most recent call last): + ... + TypeError: 'float' object cannot be interpreted as an integer + >>> binary_xor("0", "1") + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' + """ + if a < 0 or b < 0: + raise ValueError("the value of both input must be positive") + + a_binary = str(bin(a))[2:] # remove the leading "0b" + b_binary = str(bin(b))[2:] # remove the leading "0b" + + max_len = max(len(a_binary), len(b_binary)) + + return "0b" + "".join( + str(int(char_a != char_b)) + for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len)) + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From f9b890ff71c1e27a319466d021be31b34d8ca178 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Tue, 15 Sep 2020 20:36:08 +0530 Subject: [PATCH 2/4] Updated binary_and_operator.py --- bit_manipulation/binary_and_operator.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bit_manipulation/binary_and_operator.py b/bit_manipulation/binary_and_operator.py index 15c7900c6a48..e5dffe3e31d2 100644 --- a/bit_manipulation/binary_and_operator.py +++ b/bit_manipulation/binary_and_operator.py @@ -1,3 +1,4 @@ +# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm def binary_and(a: int, b: int): """ From 33a3e115e8b988e6dfebd42e7157066e7d627d00 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Tue, 15 Sep 2020 20:38:23 +0530 Subject: [PATCH 3/4] Updated binary_xor_operator.py --- bit_manipulation/binary_xor_operator.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bit_manipulation/binary_xor_operator.py b/bit_manipulation/binary_xor_operator.py index 9f3a4063d4d9..51526a2e3c67 100644 --- a/bit_manipulation/binary_xor_operator.py +++ b/bit_manipulation/binary_xor_operator.py @@ -1,3 +1,4 @@ +# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm def binary_xor(a: int, b: int): """ From e191aa0f70a977c1f2885b2f6b150b5725cdf0c0 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Tue, 15 Sep 2020 20:58:41 +0530 Subject: [PATCH 4/4] Updated binary_xor_operator.py --- bit_manipulation/binary_xor_operator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/binary_xor_operator.py b/bit_manipulation/binary_xor_operator.py index 51526a2e3c67..32a8f272116e 100644 --- a/bit_manipulation/binary_xor_operator.py +++ b/bit_manipulation/binary_xor_operator.py @@ -9,9 +9,9 @@ def binary_xor(a: int, b: int): >>> binary_xor(25, 32) '0b111001' >>> binary_xor(37, 50) - '0b10111' + '0b010111' >>> binary_xor(21, 30) - '0b1011' + '0b01011' >>> binary_xor(58, 73) '0b1110011' >>> binary_xor(0, 255)