Consider this code example:
char c = 0xff; char mask = 0xfe; switch ((unsigned)(c & mask)) { case -2: break; default: }
Suppose CHAR_BIT = 8, and the implementation assigned to c and the mask is the interpretation of bit patterns: 11111111 and 11111110, and negative zeros are allowed. Therefore, the behavior of this code:
if char is signed and the implementation uses 2 additions, c = -1, mask = -2, c & mask = -2, (unsigned)(c & mask) = UINT_MAX - 1 .
if char is signed and the implementation uses 1 complement, c = 0, mask = -1, c & mask = 0, (unsigned)(c & mask) = 0 . c is zero, not negative zero, because C does not allow you to create a negative zero through assignment.
if char is signed and the implementation uses a signed value, c = -127, mask = -126, c & mask = -126, (unsigned)(c & mask) = UINT_MAX - 125
if unsigned char c = 255, mask = 254, c & mask = 254, (unsigned)(c & mask) = 254
The case -2 constant is converted to the same type as the control expression, so the value of UINT_MAX - 1 . Therefore, this will only match if the char is signed, and the implementation uses 2 additions.
Is this correct according to the C standard, or are there additional assumptions that need to be added?