From 3ffc8976381bff452a0f2e2a59894777a0aa979d Mon Sep 17 00:00:00 2001 From: Tapas Singhal <98687345+Shocker-lov-t@users.noreply.github.com> Date: Thu, 26 Oct 2023 22:26:37 +0530 Subject: [PATCH 1/4] Create find_previous_power_of_two.py --- .../find_previous_power_of_two.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 bit_manipulation/find_previous_power_of_two.py diff --git a/bit_manipulation/find_previous_power_of_two.py b/bit_manipulation/find_previous_power_of_two.py new file mode 100644 index 000000000000..afa49bbcfcab --- /dev/null +++ b/bit_manipulation/find_previous_power_of_two.py @@ -0,0 +1,45 @@ +# https://stackoverflow.com/questions/1322510/given-an-integer-how-do-i-find-the-next-largest-power-of-two-using-bit-twiddlin + +def find_previous_power_of_two(number: int) -> int: + """ + Find the largest power of two that is less than or equal to a given integer. + + >>> find_previous_power_of_two(10) + 8 + >>> find_previous_power_of_two(16) + 16 + >>> find_previous_power_of_two(5) + 4 + >>> find_previous_power_of_two(1) + 1 + >>> find_previous_power_of_two(0) + 0 + >>> find_previous_power_of_two(-5) + Traceback (most recent call last): + ... + ValueError: Input must be a non-negative integer + >>> find_previous_power_of_two(10.5) + Traceback (most recent call last): + ... + TypeError: Input must be an integer + """ + + if not isinstance(number, int): + raise TypeError("Input must be an integer") + + if number < 0: + raise ValueError("Input must be a non-negative integer") + + if number == 0: + return 0 + + power = 1 + while power <= number: + power <<= 1 # Equivalent to multiplying by 2 + + return power >> 1 + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 2970976ee70580c64987ec6128e9161b3623d475 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 Oct 2023 16:58:36 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/find_previous_power_of_two.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bit_manipulation/find_previous_power_of_two.py b/bit_manipulation/find_previous_power_of_two.py index afa49bbcfcab..8acbc38d71c3 100644 --- a/bit_manipulation/find_previous_power_of_two.py +++ b/bit_manipulation/find_previous_power_of_two.py @@ -1,5 +1,6 @@ # https://stackoverflow.com/questions/1322510/given-an-integer-how-do-i-find-the-next-largest-power-of-two-using-bit-twiddlin + def find_previous_power_of_two(number: int) -> int: """ Find the largest power of two that is less than or equal to a given integer. @@ -39,6 +40,7 @@ def find_previous_power_of_two(number: int) -> int: return power >> 1 + if __name__ == "__main__": import doctest From a4a0b9d761e1dc1cc0fe1ee959c59276c88d9531 Mon Sep 17 00:00:00 2001 From: Tapas Singhal <98687345+Shocker-lov-t@users.noreply.github.com> Date: Sun, 29 Oct 2023 14:48:03 +0530 Subject: [PATCH 3/4] Update find_previous_power_of_two.py This change avoids the unnecessary left shift operation --- bit_manipulation/find_previous_power_of_two.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/find_previous_power_of_two.py b/bit_manipulation/find_previous_power_of_two.py index 8acbc38d71c3..42bde57c0054 100644 --- a/bit_manipulation/find_previous_power_of_two.py +++ b/bit_manipulation/find_previous_power_of_two.py @@ -38,7 +38,7 @@ def find_previous_power_of_two(number: int) -> int: while power <= number: power <<= 1 # Equivalent to multiplying by 2 - return power >> 1 + return power >> 1 if number > 1 else 1 if __name__ == "__main__": From 7b8af150b1312f9f028ead1aefeca7b1ae311073 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 29 Oct 2023 11:21:00 +0100 Subject: [PATCH 4/4] Update find_previous_power_of_two.py --- .../find_previous_power_of_two.py | 27 ++++--------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/bit_manipulation/find_previous_power_of_two.py b/bit_manipulation/find_previous_power_of_two.py index 42bde57c0054..8ac74ac98478 100644 --- a/bit_manipulation/find_previous_power_of_two.py +++ b/bit_manipulation/find_previous_power_of_two.py @@ -1,20 +1,10 @@ -# https://stackoverflow.com/questions/1322510/given-an-integer-how-do-i-find-the-next-largest-power-of-two-using-bit-twiddlin - - def find_previous_power_of_two(number: int) -> int: """ Find the largest power of two that is less than or equal to a given integer. + https://stackoverflow.com/questions/1322510 - >>> find_previous_power_of_two(10) - 8 - >>> find_previous_power_of_two(16) - 16 - >>> find_previous_power_of_two(5) - 4 - >>> find_previous_power_of_two(1) - 1 - >>> find_previous_power_of_two(0) - 0 + >>> [find_previous_power_of_two(i) for i in range(18)] + [0, 1, 2, 2, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 16, 16] >>> find_previous_power_of_two(-5) Traceback (most recent call last): ... @@ -22,22 +12,15 @@ def find_previous_power_of_two(number: int) -> int: >>> find_previous_power_of_two(10.5) Traceback (most recent call last): ... - TypeError: Input must be an integer + ValueError: Input must be a non-negative integer """ - - if not isinstance(number, int): - raise TypeError("Input must be an integer") - - if number < 0: + if not isinstance(number, int) or number < 0: raise ValueError("Input must be a non-negative integer") - if number == 0: return 0 - power = 1 while power <= number: power <<= 1 # Equivalent to multiplying by 2 - return power >> 1 if number > 1 else 1