From 686e5ef3b5fbf6103e4e2e97dafd9b8c8a5f3081 Mon Sep 17 00:00:00 2001 From: Firejay3 <68265194+Firejay3@users.noreply.github.com> Date: Tue, 18 Aug 2020 17:29:59 +0800 Subject: [PATCH 1/8] added bitwise binary OR operator --- bit_manipulation/binary_OR_operator.py | 72 ++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 bit_manipulation/binary_OR_operator.py diff --git a/bit_manipulation/binary_OR_operator.py b/bit_manipulation/binary_OR_operator.py new file mode 100644 index 000000000000..ddcd19fa5699 --- /dev/null +++ b/bit_manipulation/binary_OR_operator.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- +""" +https://www.tutorialspoint.com/python3/bitwise_operators_example.htm +This function takes in 2 integer, convert them to binary and returns a binary +in str format that is the result of an Binary OR operation from the 2 integer +input. + +Returns a binary resulted from 2 integer input in str format +>>> Binary_OR_Operator(25,32) +'0b111001' + +>>> Binary_OR_Operator(37,50) +'0b110111' + +>>> Binary_OR_Operator(21,30) +'0b11111' + +>>> Binary_OR_Operator(58,73) +'0b1111011' +""" + + +def Binary_OR_Operator (a : int, b : int): + + if type(a) == float or type(b) == float: + raise TypeError("'Float' object cannot be implemented as an integer") + if type(a) == str or type(b) == str: + raise TypeError("'str' object cannot be implemented as an integer") + if a <0 or b < 0: + raise ValueError("the value of both input must be positive") + """ + a_binary and b_binary are the binary of a and b in str format + """ + a_binary = convert_to_binary(a) + b_binary = convert_to_binary(b) + binary = [] + if len(a_binary) > len(b_binary): + greater = len(a_binary) + for i in range(greater - len(b_binary)): + b_binary= "0" + b_binary + else: + greater = len(b_binary) + for i in range(greater - len(a_binary)): + a_binary= "0" + a_binary + for i in range(greater): + if a_binary[i] == "1" or b_binary[i] == "1": + binary.append("1") + else: + binary.append("0") + return "0b" + "".join(binary) + + +""" +The function below converts the integer input from decimal to binary and +returns the binary in str format +""" +def convert_to_binary(num: int)-> str: + + first = True + while num > 0: + if first: + binarynumber = str(num%2) + first = False + else: + binarynumber = str(num%2) + binarynumber + num = int(num / 2) + return binarynumber + +if __name__ == "__main__": + import doctest + + doctest.testmod() \ No newline at end of file From 2b0ee30de3a37338ec8e66f1c72b4828b9e05ea0 Mon Sep 17 00:00:00 2001 From: Firejay3 <68265194+Firejay3@users.noreply.github.com> Date: Tue, 18 Aug 2020 17:37:09 +0800 Subject: [PATCH 2/8] Rename binary_OR_operator.py to binary_or_operator.py --- .../{binary_OR_operator.py => binary_or_operator.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename bit_manipulation/{binary_OR_operator.py => binary_or_operator.py} (95%) diff --git a/bit_manipulation/binary_OR_operator.py b/bit_manipulation/binary_or_operator.py similarity index 95% rename from bit_manipulation/binary_OR_operator.py rename to bit_manipulation/binary_or_operator.py index ddcd19fa5699..392727af26a3 100644 --- a/bit_manipulation/binary_OR_operator.py +++ b/bit_manipulation/binary_or_operator.py @@ -69,4 +69,4 @@ def convert_to_binary(num: int)-> str: if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From 2ba22f16bc066fd2eafab187e450e3d22cb93fa5 Mon Sep 17 00:00:00 2001 From: Firejay3 <68265194+Firejay3@users.noreply.github.com> Date: Tue, 18 Aug 2020 22:45:11 +0800 Subject: [PATCH 3/8] Update binary_or_operator.py --- bit_manipulation/binary_or_operator.py | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/bit_manipulation/binary_or_operator.py b/bit_manipulation/binary_or_operator.py index 392727af26a3..f79aceb028b5 100644 --- a/bit_manipulation/binary_or_operator.py +++ b/bit_manipulation/binary_or_operator.py @@ -22,17 +22,17 @@ def Binary_OR_Operator (a : int, b : int): - if type(a) == float or type(b) == float: + if isinstance(a, float) or isinstance(b, float): raise TypeError("'Float' object cannot be implemented as an integer") - if type(a) == str or type(b) == str: + if isinstance(a, str) or isinstance(b,str): raise TypeError("'str' object cannot be implemented as an integer") - if a <0 or b < 0: + if a < 0 or b < 0: raise ValueError("the value of both input must be positive") """ a_binary and b_binary are the binary of a and b in str format """ - a_binary = convert_to_binary(a) - b_binary = convert_to_binary(b) + a_binary = str(bin(a))[2:] + b_binary = str(bin(b))[2:] binary = [] if len(a_binary) > len(b_binary): greater = len(a_binary) @@ -50,21 +50,6 @@ def Binary_OR_Operator (a : int, b : int): return "0b" + "".join(binary) -""" -The function below converts the integer input from decimal to binary and -returns the binary in str format -""" -def convert_to_binary(num: int)-> str: - - first = True - while num > 0: - if first: - binarynumber = str(num%2) - first = False - else: - binarynumber = str(num%2) + binarynumber - num = int(num / 2) - return binarynumber if __name__ == "__main__": import doctest From 9146d42f8892a69688e837143206ee2595dd19c4 Mon Sep 17 00:00:00 2001 From: Firejay3 <68265194+Firejay3@users.noreply.github.com> Date: Tue, 18 Aug 2020 23:51:32 +0800 Subject: [PATCH 4/8] Update binary_or_operator.py --- bit_manipulation/binary_or_operator.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/bit_manipulation/binary_or_operator.py b/bit_manipulation/binary_or_operator.py index f79aceb028b5..dc87e81f6293 100644 --- a/bit_manipulation/binary_or_operator.py +++ b/bit_manipulation/binary_or_operator.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """ https://www.tutorialspoint.com/python3/bitwise_operators_example.htm This function takes in 2 integer, convert them to binary and returns a binary @@ -6,25 +5,24 @@ input. Returns a binary resulted from 2 integer input in str format ->>> Binary_OR_Operator(25,32) +>>> binary_or(25,32) '0b111001' ->>> Binary_OR_Operator(37,50) +>>> binary_or(37,50) '0b110111' ->>> Binary_OR_Operator(21,30) +>>> binary_or(21,30) '0b11111' ->>> Binary_OR_Operator(58,73) +>>> binary_or(58,73) '0b1111011' """ -def Binary_OR_Operator (a : int, b : int): - +def binary_or(a : int, b : int): if isinstance(a, float) or isinstance(b, float): raise TypeError("'Float' object cannot be implemented as an integer") - if isinstance(a, str) or isinstance(b,str): + if isinstance(a, str) or isinstance(b, str): raise TypeError("'str' object cannot be implemented as an integer") if a < 0 or b < 0: raise ValueError("the value of both input must be positive") @@ -36,22 +34,18 @@ def Binary_OR_Operator (a : int, b : int): binary = [] if len(a_binary) > len(b_binary): greater = len(a_binary) - for i in range(greater - len(b_binary)): - b_binary= "0" + b_binary + b_binary = b_binary.zfill(greater) else: greater = len(b_binary) - for i in range(greater - len(a_binary)): - a_binary= "0" + a_binary + a_binary = a_binary.zfill(greater) for i in range(greater): if a_binary[i] == "1" or b_binary[i] == "1": binary.append("1") else: binary.append("0") return "0b" + "".join(binary) - if __name__ == "__main__": import doctest - doctest.testmod() From b0c694cd1fc689cee042be31daae94fa9d17039c Mon Sep 17 00:00:00 2001 From: Firejay3 <68265194+Firejay3@users.noreply.github.com> Date: Wed, 19 Aug 2020 01:45:15 +0800 Subject: [PATCH 5/8] Update bit_manipulation/binary_or_operator.py Co-authored-by: Christian Clauss --- bit_manipulation/binary_or_operator.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/bit_manipulation/binary_or_operator.py b/bit_manipulation/binary_or_operator.py index dc87e81f6293..e429fc1645e1 100644 --- a/bit_manipulation/binary_or_operator.py +++ b/bit_manipulation/binary_or_operator.py @@ -32,12 +32,9 @@ def binary_or(a : int, b : int): a_binary = str(bin(a))[2:] b_binary = str(bin(b))[2:] binary = [] - if len(a_binary) > len(b_binary): - greater = len(a_binary) - b_binary = b_binary.zfill(greater) - else: - greater = len(b_binary) - a_binary = a_binary.zfill(greater) + max_len = max(len(a_binary), len(b_binary)) + a_binary = a_binary.zfill(max_len) + b_binary = b_binary.zfill(max_len) for i in range(greater): if a_binary[i] == "1" or b_binary[i] == "1": binary.append("1") From fc93150618f6bf6519faba028602152c2cd1be02 Mon Sep 17 00:00:00 2001 From: Firejay3 <68265194+Firejay3@users.noreply.github.com> Date: Wed, 19 Aug 2020 02:29:15 +0800 Subject: [PATCH 6/8] Update binary_or_operator.py --- bit_manipulation/binary_or_operator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/binary_or_operator.py b/bit_manipulation/binary_or_operator.py index e429fc1645e1..78d6befe3d37 100644 --- a/bit_manipulation/binary_or_operator.py +++ b/bit_manipulation/binary_or_operator.py @@ -35,8 +35,8 @@ def binary_or(a : int, b : int): max_len = max(len(a_binary), len(b_binary)) a_binary = a_binary.zfill(max_len) b_binary = b_binary.zfill(max_len) - for i in range(greater): - if a_binary[i] == "1" or b_binary[i] == "1": + for char_a, char_b in zip(a_binary, b_binary): + if char_a == "1" or char_b == "1": binary.append("1") else: binary.append("0") From 42b5a9e2b2de1592d2b85bc600c06378aaed5012 Mon Sep 17 00:00:00 2001 From: Firejay3 <68265194+Firejay3@users.noreply.github.com> Date: Wed, 19 Aug 2020 02:44:03 +0800 Subject: [PATCH 7/8] Update binary_or_operator.py --- bit_manipulation/binary_or_operator.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bit_manipulation/binary_or_operator.py b/bit_manipulation/binary_or_operator.py index 78d6befe3d37..f614669fab6b 100644 --- a/bit_manipulation/binary_or_operator.py +++ b/bit_manipulation/binary_or_operator.py @@ -36,10 +36,7 @@ def binary_or(a : int, b : int): a_binary = a_binary.zfill(max_len) b_binary = b_binary.zfill(max_len) for char_a, char_b in zip(a_binary, b_binary): - if char_a == "1" or char_b == "1": - binary.append("1") - else: - binary.append("0") + binary.append(str(int(char_a == "1" or char_b == "1"))) return "0b" + "".join(binary) From 4051f2693c11b5c27f4fe6842c13e4eeb231c318 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 18 Aug 2020 21:26:12 +0200 Subject: [PATCH 8/8] Nice!! --- bit_manipulation/binary_or_operator.py | 71 ++++++++++++++------------ 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/bit_manipulation/binary_or_operator.py b/bit_manipulation/binary_or_operator.py index f614669fab6b..e83a86d6a8bc 100644 --- a/bit_manipulation/binary_or_operator.py +++ b/bit_manipulation/binary_or_operator.py @@ -1,45 +1,48 @@ -""" -https://www.tutorialspoint.com/python3/bitwise_operators_example.htm -This function takes in 2 integer, convert them to binary and returns a binary -in str format that is the result of an Binary OR operation from the 2 integer -input. +# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm -Returns a binary resulted from 2 integer input in str format ->>> binary_or(25,32) -'0b111001' ->>> binary_or(37,50) -'0b110111' - ->>> binary_or(21,30) -'0b11111' - ->>> binary_or(58,73) -'0b1111011' -""" - - -def binary_or(a : int, b : int): - if isinstance(a, float) or isinstance(b, float): - raise TypeError("'Float' object cannot be implemented as an integer") - if isinstance(a, str) or isinstance(b, str): - raise TypeError("'str' object cannot be implemented as an integer") - if a < 0 or b < 0: - raise ValueError("the value of both input must be positive") +def binary_or(a: int, b: int): """ - a_binary and b_binary are the binary of a and b in str format + Take in 2 integers, convert them to binary, and return a binary number that is the + result of a binary or operation on the integers provided. + + >>> binary_or(25, 32) + '0b111001' + >>> binary_or(37, 50) + '0b110111' + >>> binary_or(21, 30) + '0b11111' + >>> binary_or(58, 73) + '0b1111011' + >>> binary_or(0, 255) + '0b11111111' + >>> binary_or(0, 256) + '0b100000000' + >>> binary_or(0, -1) + Traceback (most recent call last): + ... + ValueError: the value of both input must be positive + >>> binary_or(0, 1.1) + Traceback (most recent call last): + ... + TypeError: 'float' object cannot be interpreted as an integer + >>> binary_or("0", "1") + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' """ - a_binary = str(bin(a))[2:] + 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:] - binary = [] max_len = max(len(a_binary), len(b_binary)) - a_binary = a_binary.zfill(max_len) - b_binary = b_binary.zfill(max_len) - for char_a, char_b in zip(a_binary, b_binary): - binary.append(str(int(char_a == "1" or char_b == "1"))) - return "0b" + "".join(binary) + return "0b" + "".join( + str(int("1" in (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()