From cd110ab5b0a75cd437d1c2e9330c43bb480a4e08 Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Thu, 14 Jan 2021 16:40:32 +0530 Subject: [PATCH 1/4] add reverse_bits.py --- bit_manipulation/reverse_bits.py | 67 ++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 bit_manipulation/reverse_bits.py diff --git a/bit_manipulation/reverse_bits.py b/bit_manipulation/reverse_bits.py new file mode 100644 index 000000000000..5619b8b9e0b1 --- /dev/null +++ b/bit_manipulation/reverse_bits.py @@ -0,0 +1,67 @@ +def get_reverse_bit_string(bn : int) -> str: + """ return the bit string of an interger + """ + bit_string = "" + for trk in range(0, 32): + bit_string += str(bn % 2) + bn = bn >> 1 + return bit_string + + +def reverse_bit(n: int) -> str: + """ + Take in an 32 bit integer, reverse its bits, + return a string of reverse bits + + result of a reverse_bit and operation on the integer provided. + + >>> reverse_bit(25) + '00000000000000000000000000011001' + >>> reverse_bit(37) + '00000000000000000000000000100101' + >>> reverse_bit(21) + '00000000000000000000000000010101' + >>> reverse_bit(58) + '00000000000000000000000000111010' + >>> reverse_bit(0) + '00000000000000000000000000000000' + >>> reverse_bit(256) + '00000000000000000000000100000000' + >>> reverse_bit(-1) + Traceback (most recent call last): + ... + ValueError: the value of input must be positive + + >>> reverse_bit(1.1) + Traceback (most recent call last): + ... + TypeError: Input value must be a 'int' type + + >>> reverse_bit("0") + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' + """ + if n < 0: + raise ValueError("the value of input must be positive") + elif isinstance(n, float): + raise TypeError("Input value must be a 'int' type") + elif isinstance(n, str): + raise TypeError("'<' not supported between instances of 'str' and 'int'") + ans = 0 + # iterator over [1 to 32],since we are dealing with 32 bit integer + for i in range(1, 33): + # left shift the bits by unity + ans = ans << 1 + # get the end bit + k = n % 2 + # right shif the bits by unity + n = n >> 1 + # add that bit to our ans + ans = ans | k + return get_reverse_bit_string(ans) + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 3e9f11ecaa346ac5e8701aeb3539053fc7c0676b Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Fri, 15 Jan 2021 19:56:56 +0530 Subject: [PATCH 2/4] check --- bit_manipulation/binary_xor_operator_new.py | 52 +++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 bit_manipulation/binary_xor_operator_new.py diff --git a/bit_manipulation/binary_xor_operator_new.py b/bit_manipulation/binary_xor_operator_new.py new file mode 100644 index 000000000000..6f8962192ad8 --- /dev/null +++ b/bit_manipulation/binary_xor_operator_new.py @@ -0,0 +1,52 @@ +# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm + + +def binary_xor(a: int, b: int) -> str: + """ + 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) + '0b010111' + >>> binary_xor(21, 30) + '0b01011' + >>> 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 dc4c3a795b390319de708db3515dbb3105a13211 Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Sun, 17 Jan 2021 10:36:43 +0530 Subject: [PATCH 3/4] Delete binary_xor_operator_new.py --- bit_manipulation/binary_xor_operator_new.py | 52 --------------------- 1 file changed, 52 deletions(-) delete mode 100644 bit_manipulation/binary_xor_operator_new.py diff --git a/bit_manipulation/binary_xor_operator_new.py b/bit_manipulation/binary_xor_operator_new.py deleted file mode 100644 index 6f8962192ad8..000000000000 --- a/bit_manipulation/binary_xor_operator_new.py +++ /dev/null @@ -1,52 +0,0 @@ -# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm - - -def binary_xor(a: int, b: int) -> str: - """ - 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) - '0b010111' - >>> binary_xor(21, 30) - '0b01011' - >>> 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 7566e751a2d42fc22855757a846372f99c5678e7 Mon Sep 17 00:00:00 2001 From: xcodz-dot Date: Sun, 17 Jan 2021 11:00:48 +0530 Subject: [PATCH 4/4] Fix All the errors --- bit_manipulation/reverse_bits.py | 52 +++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/bit_manipulation/reverse_bits.py b/bit_manipulation/reverse_bits.py index 5619b8b9e0b1..55608ae12908 100644 --- a/bit_manipulation/reverse_bits.py +++ b/bit_manipulation/reverse_bits.py @@ -1,14 +1,31 @@ -def get_reverse_bit_string(bn : int) -> str: - """ return the bit string of an interger +def get_reverse_bit_string(number: int) -> str: """ + return the bit string of an integer + + >>> get_reverse_bit_string(9) + '10010000000000000000000000000000' + >>> get_reverse_bit_string(43) + '11010100000000000000000000000000' + >>> get_reverse_bit_string(2873) + '10011100110100000000000000000000' + >>> get_reverse_bit_string("this is not a number") + Traceback (most recent call last): + ... + TypeError: operation can not be conducted on a object of type str + """ + if not isinstance(number, int): + raise TypeError( + "operation can not be conducted on a object of type " + f"{type(number).__name__}" + ) bit_string = "" - for trk in range(0, 32): - bit_string += str(bn % 2) - bn = bn >> 1 + for _ in range(0, 32): + bit_string += str(number % 2) + number = number >> 1 return bit_string -def reverse_bit(n: int) -> str: +def reverse_bit(number: int) -> str: """ Take in an 32 bit integer, reverse its bits, return a string of reverse bits @@ -42,26 +59,27 @@ def reverse_bit(n: int) -> str: ... TypeError: '<' not supported between instances of 'str' and 'int' """ - if n < 0: + if number < 0: raise ValueError("the value of input must be positive") - elif isinstance(n, float): + elif isinstance(number, float): raise TypeError("Input value must be a 'int' type") - elif isinstance(n, str): + elif isinstance(number, str): raise TypeError("'<' not supported between instances of 'str' and 'int'") - ans = 0 + result = 0 # iterator over [1 to 32],since we are dealing with 32 bit integer - for i in range(1, 33): + for _ in range(1, 33): # left shift the bits by unity - ans = ans << 1 + result = result << 1 # get the end bit - k = n % 2 - # right shif the bits by unity - n = n >> 1 + end_bit = number % 2 + # right shift the bits by unity + number = number >> 1 # add that bit to our ans - ans = ans | k - return get_reverse_bit_string(ans) + result = result | end_bit + return get_reverse_bit_string(result) if __name__ == "__main__": import doctest + doctest.testmod()