In this example, a bitwise complement of
port is first computed and then shifted 4 bits to the right. If both of these operations are performed on an 8-bit unsigned integer, then
result_8 will have the value
port is first promoted to a
signed int, with the following results (on a typical architecture where type
int is 32 bits wide):
Whether or not value is negative is implementation-defined.
In this compliant solution, the bitwise complement of
port is converted back to 8 bits. Consequently,
result_8 is assigned the expected value of
Bitwise operations on shorts and chars can produce incorrect data.
|LANG.CAST.RIP||Risky integer promotion|
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
|SEI CERT C++ Coding Standard||VOID EXP15-CPP. Beware of integer promotion when performing bitwise operations on chars or shorts|