...
This code produces the two's complement of x for implementations where unsigned long have a precision of 32 bits. However, on implementations where unsigned long is larger than unsigned int, this code has incorrect behavior. If unsigned long has a precision of 64 bits, this code does not produce the two's complement of x. Because and unsigned int has a precision of 32 bits, only the lower order 32-bits are set, and the resulting value of mask is 0x00000000FFFFFFFF.
...
| Code Block | ||
|---|---|---|
| ||
const unsigned long mask = (1 << ((sizeof(unsigned long) * CHAR_BIT) - 1))
unsigned long x;
/* Initialize x */
x = (x ^ mask) + 1;
|
...