Although many common implementations use a two's complement representation of signed integers, the C Standard declares such use as implementation-defined and allows all of the following representations:
- sign Sign and magnitude
- twoTwo's complement
- onesOne's complement
This is a specific example of MSC14-C. Do not introduce unnecessary platform dependencies.
...
One way to check whether a number is even or odd is to examine the least significant bit, but the results will be inconsistent. Specifically, this example gives unexpected behavior on all onesone's complement implementations:
Code Block | ||||
---|---|---|---|---|
| ||||
int value; if (scanf("%d", &value) == 1) { if (value & 0x1 != 0) { /* doDo something if value is odd */ } } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
int value; if (scanf("%d", &value) == 1) { if (value % 2 != 0) { /* doDo something if value is odd */ } } |
...