Skip to content

Add largest power of 2 less than or equal to a given number #9409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions bit_manipulation/Added largest_pow_of_two_le_num.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
Author : Dwarkadhish Kamthane
Date : October 2 2023
Task:
Find largest power of 2 less than or equal to a given number
"""


def largest_power_of_two(number: int) -> int:
"""
Return the largest power of two less than or equal to a number.
>>> largest_power_of_two(0)
0
>>> largest_power_of_two(1)
1
>>> largest_power_of_two(-1)
0
>>> largest_power_of_two(3)
2
>>> largest_power_of_two(15)
8
>>> largest_power_of_two(99)
64
>>> largest_power_of_two(178)
128
>>> largest_power_of_two(999999)
524288
>>> largest_power_of_two(99.9)
Traceback (most recent call last):
...
TypeError: Input value must be a 'int' type
"""
if isinstance(number, float):
raise TypeError("Please enter an int type input")
if (number <= 0):
return 0
x = 1
while (x << 1) <= number:
x <<= 1
return x


if __name__ == "__main__":
import doctest

doctest.testmod()