interview-questions-in-javascript icon indicating copy to clipboard operation
interview-questions-in-javascript copied to clipboard

isPowerOfTwo is absolutely incorrect solution in JS

Open eGust opened this issue 5 years ago • 0 comments

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"

eGust avatar Jun 23 '19 13:06 eGust