Signed integer arithmetic uses the same bit operations as unsigned arithmetic, while interpreting representations with a high-order 1 bit as negative under two’s complement. section 5.7.5
Signed overflow changes the rules
An unsigned counter eventually wraps from UINT_MAX to 0. A signed counter that overflows instead enters undefined behavior, allowing the compiler to assume the overflow never occurs and potentially transform the loop into an infinite loop. section 5.7.5
bool is_negative(unsigned a) {
constexpr unsigned int_max = UINT_MAX/2;
return a > int_max;
}
bool is_signed_less(unsigned a, unsigned b) {
if (is_negative(a) != is_negative(b)) return a > b;
else return a < b;
}
Compare unsigned values as signed values by detecting the sign bit and reversing the ordering when exactly one operand is negative. section 5.7.5

