-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Largest power of 2 #9464
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
Closed
Largest power of 2 #9464
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2d5e8f2
added largest_power_of_2
siddwarr 8be0479
updated_largest_power_of_2
siddwarr 71e4afb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 81d29a0
updated largest_power_of_2
siddwarr 23af638
Merge branch 'largest_power_of_2' of https://github.com/siddwarr/Pyth…
siddwarr 67dc7d9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a8be9c2
updated largest_power_of_2
siddwarr 923843b
Merge branch 'largest_power_of_2' of https://github.com/siddwarr/Pyth…
siddwarr 834faa0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 24a040a
updated largest_power_of_2
siddwarr d0012f9
updated largest_power_of_2
siddwarr 7aef7b7
updated largest_power_of_2
siddwarr 2b2cb63
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2864ef8
updated code
siddwarr 6f9b63b
Merge branch 'largest_power_of_2' of https://github.com/siddwarr/Pyth…
siddwarr e6ea6a8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b1ac1f1
updated code
siddwarr 6cbd413
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1c8f843
updated code
siddwarr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
def largest_power_of_2(number: int) -> int: | ||
""" | ||
returns the largest power of 2 that is less than | ||
or equal to the given number | ||
The way this works is that we shift the binary form of the number | ||
to the right until we reach the last set bit | ||
Using the number of times we had to shift to find the last set bit | ||
we find the 2**(no of times shifted) which will be the ans | ||
>>> largest_power_of_2(0) | ||
0 | ||
>>> largest_power_of_2(1) | ||
1 | ||
>>> largest_power_of_2(3) | ||
2 | ||
>>> largest_power_of_2(15) | ||
8 | ||
>>> largest_power_of_2(99) | ||
64 | ||
>>> largest_power_of_2(178) | ||
128 | ||
>>> largest_power_of_2(999999) | ||
524288 | ||
""" | ||
|
||
if number == 0: | ||
return 0 | ||
else: | ||
last_set_bit = 0 | ||
while number: | ||
last_set_bit += 1 | ||
number >>= 1 | ||
return 2 ** (last_set_bit - 1) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
bit_manipulation/largest_power_of_2.py
, please provide doctest for the functionlargest_power_of_2