forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext_power_of_two.py
More file actions
55 lines (48 loc) · 1.17 KB
/
Copy pathnext_power_of_two.py
File metadata and controls
55 lines (48 loc) · 1.17 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
"""
Author : Basuki Nath
Date : 2025-10-04
Utility to compute the next power of two greater than or equal to n.
"""
def next_power_of_two(n: int) -> int:
"""
Return the smallest power of two >= n for positive integers.
>>> next_power_of_two(1)
1
>>> next_power_of_two(2)
2
>>> next_power_of_two(3)
4
>>> next_power_of_two(5)
8
>>> next_power_of_two(1025)
2048
>>> next_power_of_two(0)
Traceback (most recent call last):
...
ValueError: n must be positive
>>> next_power_of_two(-3)
Traceback (most recent call last):
...
ValueError: n must be positive
"""
if n <= 0:
raise ValueError("n must be positive")
# If already a power of two, return it
if n & (n - 1) == 0:
return n
# Otherwise, fill bits to the right
v = n - 1
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
# for very large ints, continue shifting using Python's arbitrary precision
shift = 32
while (1 << shift) <= v:
v |= v >> shift
shift <<= 1
return v + 1
if __name__ == "__main__":
import doctest
doctest.testmod()