...
One way to check whether a number is even or odd is to examine the least significant bit. This will give inconsistent results. Specifically, this example will give unexpected behavior on all ones' complement implementations.
Code Block | ||||
---|---|---|---|---|
| ||||
int value; if (scanf("%d", &value) == 1) { if (value & 0x1 == 1) { /* do something if value is odd */ } } |
...
The same thing can be achieved compliantly using the modulo operator.
Code Block | ||||
---|---|---|---|---|
| ||||
int value; if (scanf("%d", &value) == 1) { if (value % 2 == 1) { /* do something if value is odd */ } } |
...