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

shares: powersOf2 test should put less expensive comparison before AND and SUBTRACTION #40

Closed
4 tasks done
odeke-em opened this issue Feb 29, 2024 · 1 comment
Closed
4 tasks done
Labels
bug Something isn't working external

Comments

@odeke-em
Copy link
Contributor

Summary of Bug

If we examine this code to determine if a value is a power of 2

return input&(input-1) == 0 && input != 0

notice that the test for if a value is 0 comes after the more expensive subtraction after an AND but also we need to simply check that the value is greater than 0 because that test fails anyways for negative powers of 2 for example -4, -8, -2 etc

Suggestion

return input > 0 && input&(input-1) == 0

/cc @elias-orijtech

Version

4135f35

Steps to Reproduce

Please follow the steps that I reported

For Admin Use

  • Not duplicate issue
  • Appropriate labels applied
  • Appropriate contributors tagged
  • Contributor assigned/self-assigned
@odeke-em odeke-em added the bug Something isn't working label Feb 29, 2024
@odeke-em
Copy link
Contributor Author

Actually nevermind, it is a non-issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working external
Projects
None yet
Development

No branches or pull requests

1 participant