Skip to content
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

feat: add next power of 2 algorithm #193

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

vcnovaes
Copy link
Contributor

@vcnovaes vcnovaes commented Oct 13, 2023

This PR add an implementation to find the next power of 2 using bit manipulation

@vcnovaes
Copy link
Contributor Author

Please merge it under hacktoberfest

Copy link
Contributor

@appgurueu appgurueu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise looks fine.

*/

export const findNextPowerOf2 = (n: number): number => {
n !== 0 ? n-- : n;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be written more readably as if (n !== 0) n--.


describe("find next power of two", () => {
test.each([
[0, 2],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems odd to me. Shouldn't the next power of two be 2⁰ = 1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants