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

isPowerOfTwo is absolutely incorrect solution in JS #34

Open
eGust opened this issue Jun 23, 2019 · 0 comments
Open

isPowerOfTwo is absolutely incorrect solution in JS #34

eGust opened this issue Jun 23, 2019 · 0 comments

Comments

@eGust
Copy link

eGust commented Jun 23, 2019

5.1 is a classic solution in most languages that have real integer types. Unfortunately, JS number is not a real integer. It will fall back to 32-bit signed integer when doing bitwise operations.

Just simply try this:

isPowerOfTwo(2 ** 43 - 2 ** 40)
// true
(2 ** 43 - 2 ** 40).toString(16)
// "70000000000"
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

No branches or pull requests

1 participant