Skip to content

best::count_zeros doesn't handle integer promotion properly #27

@sarah-quinones

Description

@sarah-quinones

it currently returns

return std::popcount(~to_unsigned(x));

when x is smaller than int, e.g., short. to_unsigned(x) returns a value of type unsigned short. using it as an operand of an arithmetic operation ~x, it first gets promoted to int, then the operation gets applied, returning another int, which causes the call to std::popcount to fail since it's a signed integer type.

to fix this we could write it as

auto u = to_unsigned(x);
using unsigned = decltype(u);

return std::popcount(static_cast<unsigned>(~u));

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