interview-questions-in-javascript
interview-questions-in-javascript copied to clipboard
isPowerOfTwo is absolutely incorrect solution in JS
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"