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 and magnitude
- Two's complement
- One's complement
This is a specific example of MSC14-C. Do not introduce unnecessary platform dependencies.
Noncompliant Code Example
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 one's complement implementations:
The same thing can be achieved compliantly using the modulo operator:
Incorrect assumptions about integer representation can lead to execution of unintended code branches and other unexpected behavior.
|LDRA tool suite|
|50 S, 120 S||Partially Implemented|
|CERT_C-INT16-a||Bitwise operators shall only be applied to operands of unsigned underlying type|
|Polyspace Bug Finder|
|MISRA C:2012 Rule 10.1|
Operands shall not be of an inappropriate essential type
2940, 2941, 2942, 2943, 2945, 2946, 2947, 2948