-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathis_power_of_two.py
More file actions
100 lines (81 loc) · 2.43 KB
/
is_power_of_two.py
File metadata and controls
100 lines (81 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Author : Alexander Pantyukhin
Date : November 1, 2022
Task:
Given a positive int number. Return True if this number is power of 2
or False otherwise.
Implementation notes: Use bit manipulation.
For example if the number is the power of two it's bits representation:
n = 0..100..00
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.
"""
def is_power_of_two(number: int) -> bool:
"""
Return True if this number is power of 2 or False otherwise.
>>> is_power_of_two(0)
True
>>> is_power_of_two(1)
True
>>> is_power_of_two(2)
True
>>> is_power_of_two(4)
True
>>> is_power_of_two(6)
False
>>> is_power_of_two(8)
True
>>> is_power_of_two(17)
False
>>> is_power_of_two(-1)
Traceback (most recent call last):
...
ValueError: number must not be negative
>>> is_power_of_two(1.2)
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for &: 'float' and 'float'
# Test all powers of 2 from 0 to 10,000
>>> all(is_power_of_two(int(2 ** i)) for i in range(10000))
True
"""
if number < 0:
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()