From 291558db5e7a300bd40dc26c4e29a2be04719207 Mon Sep 17 00:00:00 2001 From: Arun Kishore Voleti <49097688+ArunKishoreVoleti@users.noreply.github.com> Date: Thu, 16 Oct 2025 15:16:39 +0530 Subject: [PATCH 1/2] Add math-based power of two check and input validation Introduced is_power_of_two_math using math.log2 for educational purposes. Added type and value checks to both implementations for robustness, raising appropriate exceptions for invalid input. --- bit_manipulation/is_power_of_two.py | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/bit_manipulation/is_power_of_two.py b/bit_manipulation/is_power_of_two.py index 023e979fe51c..ef6c4a6d77ef 100644 --- a/bit_manipulation/is_power_of_two.py +++ b/bit_manipulation/is_power_of_two.py @@ -12,6 +12,20 @@ n - 1 = 0..011..11 n & (n - 1) - no intersections = 0 + +New Implementation Author: Arun +Date: 16th October, 2025 + +Change Notes: +- Added type checks and value checks for robustness. +- Added an alternative method using math.log2 for educational purposes. + +New Implementation Details: +- The function raises a ValueError if the input number is negative. +- The function raises a TypeError if the input is not an integer. +- Uses math.log2 to get the exponent and checks if it's an integer. +- For all powers of 2, log2 will yield an integer value. +- For non-powers of 2, log2 will yield a non-integer value. """ @@ -50,8 +64,35 @@ def is_power_of_two(number: int) -> bool: raise ValueError("number must not be negative") return number & (number - 1) == 0 +def is_power_of_two_math(number: int) -> bool: + from math import log2 + """ + Alternative method using math.log2 to check if number is a power of 2. + + >>> is_power_of_two_math(0) + True + >>> is_power_of_two_math(1) + True + >>> is_power_of_two_math(2) + True + >>> is_power_of_two_math(6) + False + >>> is_power_of_two_math(16) + True + """ + if not isinstance(number, int): + raise TypeError("number must be an integer") + if number < 0: + raise ValueError("number must not be negative") + + if number == 0: + return True + + value = log2(number) + return value == int(value) if __name__ == "__main__": import doctest doctest.testmod() + From 0c7ba7067068fcd3abc37cd58a5e093be2ea40e1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 Oct 2025 09:48:26 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/is_power_of_two.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/bit_manipulation/is_power_of_two.py b/bit_manipulation/is_power_of_two.py index ef6c4a6d77ef..6c5633e2ffd5 100644 --- a/bit_manipulation/is_power_of_two.py +++ b/bit_manipulation/is_power_of_two.py @@ -64,11 +64,13 @@ def is_power_of_two(number: int) -> bool: raise ValueError("number must not be negative") return number & (number - 1) == 0 + def is_power_of_two_math(number: int) -> bool: from math import log2 + """ Alternative method using math.log2 to check if number is a power of 2. - + >>> is_power_of_two_math(0) True >>> is_power_of_two_math(1) @@ -84,15 +86,15 @@ def is_power_of_two_math(number: int) -> bool: raise TypeError("number must be an integer") if number < 0: raise ValueError("number must not be negative") - + if number == 0: return True - + value = log2(number) return value == int(value) + if __name__ == "__main__": import doctest doctest.testmod() -