Open sarah-ek opened 2 months ago
I think the easiest fix is gonna be std::popcount(to_unsigned(~to_unsigned(x))
. Casting to unsigned
isn't correct because it won't work right for 64-bit arguments. Feel free to file a pull request here.
oh oops. i haven't written c++ in a while so i forgot unsigned
is a keyword :skull:
the unsigned
in the cast refers to the type alias on the second line.
it currently returns
when
x
is smaller thanint
, e.g.,short
.to_unsigned(x)
returns a value of typeunsigned short
. using it as an operand of an arithmetic operation~x
, it first gets promoted toint
, then the operation gets applied, returning anotherint
, which causes the call tostd::popcount
to fail since it's a signed integer type.to fix this we could write it as