Skip to content

isPowerOfTwo is absolutely incorrect solution in JS #34

Open
@eGust

Description

@eGust

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions