42

The functions from P0553R4: Bit operations are constrained to only work on unsigned integers. The proposal does not give a reason for this constraint. I can see that this makes sense if the bit representation of a signed integer is not defined, but with C++20, we are guaranteed that signed integers use two's complement.

To me, it thus seems reasonable to allow e.g. std::popcount to be called with a signed integer type, as an implementation could simply cast to the corresponding unsigned type to do the bit-operation in the unsigned domain.

What is the reason for P0553R4 to add this constraint? (Is it simply missing synchronization between P0553R4 and P0907R4?)

11
  • 11
    When applying bitwise operations, such as counting the number of set bits (popcount), on signed integers, there can be unexpected behavior due to the sign bit. Specifically, the sign bit can propagate during operations, potentially leading to incorrect results or undefined behavior. Commented Jun 5, 2023 at 14:02
  • 4
    template <typename T> int spopcount(T s) { return popcount(static_cast<std::make_unsigned_t<decltype(s)>>(s)); } ... but anyway, I presume the P0553 proposal itself predates the 2s complement representation as an independent proposal. Something that could be easily "fixed" with a new small proposal. Proposals like that need someone to propose them.
    – Eljay
    Commented Jun 5, 2023 at 14:13
  • 6
    Even with the two's complement guarantee in C++20, signed integer overflows still have UB in C++20. Perhaps the reasons for the <bit> family of functions only working with unsigned types can be found in the reasoning behind that decision?
    – Ted Lyngmo
    Commented Jun 5, 2023 at 14:18
  • 9
    What is the use case for counting bits in signed values? All recommendations for using bitmasks are to always use unsigned. And on the odd chance that you find a use case, you can easily do the casting yourself.
    – BoP
    Commented Jun 5, 2023 at 14:57
  • 3
    @doug "Signed integer types have been two's compl. since c++17" - The standard didn't require signed integer types to be two's complement before C++20.
    – Ted Lyngmo
    Commented Jun 5, 2023 at 15:05

2 Answers 2

72

Pretty simple: Implicit widening conversions on unsigned types do not change the result. Implicit widening conversions on signed types (including promotions) perform sign-extension, which does change the result if the input was negative.

Having an operation whose result becomes incorrect due to integer promotion definitely falls into the "foot cannon" category.

You can still feed (bit patterns representing) negative values to popcount, but you have to take control of the conversion sequence, which helps you get the result you expect.

14
  • 6
    @TedLyngmo: language-lawyer simply isn't appropriate for a "why?" question so I ignored it. I haven't gone looking in the proposal, since OP indicates he's already done so.
    – Ben Voigt
    Commented Jun 5, 2023 at 15:20
  • 5
    The same argument can be made for std::countl_zero with unsigned types
    – Artyer
    Commented Jun 5, 2023 at 21:14
  • 6
    @Artyer: Having countl_zero(int) not be a valid overload means that countl_zero(x+1) is an error for narrow unsigned x that promotes to signed int for + (godbolt.org/z/WGvx644h5), so you realize you need static_cast<unsigned char>. BTW, even with signed types, the leading-zero count still changes when widening for non-negative integers. Also, it's the source type not the destination that determines whether zero- or sign-extension happens, so for int x, popcount(x | 1uLL) or countl_zero(x | 1uLL) will sign-extend to 64-bit (or whatever unsigned long long is). Commented Jun 6, 2023 at 4:49
  • 3
    @supercat: Commonly known as "log2()" Without a special instruction, see graphics.stanford.edu/~seander/bithacks.html#IntegerLog
    – Ben Voigt
    Commented Jun 6, 2023 at 22:14
  • 4
    @supercat: I'm not saying that you should call "log(x)/log(2.0)` to find the position of the highest non-zero bit, I'm saying that the function you are proposing is already named -- you have an efficient integer log2()
    – Ben Voigt
    Commented Jun 6, 2023 at 22:20
25

popcount counts bits, and hence takes a type intended for use as a "bit container".

  • unsigned integer types are intended for use as bit containers (or modulo-2^n values).
  • signed integer types are intended for use as numbers, somewhat more abstractly.

Yes, it's true that since C++20, twos-complement semantics are guaranteed for signed integers, but that's a non-essential detail - which is exactly why two's-complement wasn't guaranteed until C++20. And if you're not a language-lawyer, you are likely to not even be aware of this change.

So, instead of trying to be a language-lawyer/human encyclopedia - make less assumptions about the exact semantics of types which may have been defined otherwise. If you make such assumptions - you might luck out and popcount correctly; but you might just get bitten by @BenVoigt's answer.

See also the application of the same principle in my answer to this question.

Not the answer you're looking for? Browse other questions tagged or ask your own question.