Integer variables are frequently intended to represent either a numeric value or a bit collection. Numeric values should must be exclusively operated upon using arithmetic operations, while bit collections should be exclusively operated upon using logical operations. However, static analyzers are frequently unable to determine the intended use of a particular integer variable.
...
A reviewer could now recognize that the operation should also be checked for overflow. This might not have been apparent in the original, noncompliant code example. For more information, see rule " INT00NUM00-J. Ensure that integer operations do not result in Detect or prevent integer overflow."
Noncompliant Code Example (Logical Right Shift)
...
NUM01-EX0: Bitwise operations may be used to construct constant expressions.
Code Block | ||
---|---|---|
| ||
int limit = 1 << 17 - 1; // 2^17 - 1 = 131071
|
Nevertheless, as a matter of style, we recommend replacing such constant expressions with the equivalent hexadecimal constants 0x1FFFF, in this case.
Code Block | ||
---|---|---|
| ||
int limit = 1 << 17 - 10x1FFFF; // 2^17 - 1 = 131071 |
NUM01-EX1: Data that is normally treated arithmetically may be treated with bitwise operations for the purpose of serialization or deserialization. This is often required for reading or writing the data from a file or network socket. Bitwise operations are also permitted when reading or writing the data from a tightly packed data structure of bytes.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="12e85aafa06d8482-d24cbb70-470349af-bc1985c4-01644bd3ba7f2d5e77e9f4fd"><ac:plain-text-body><![CDATA[ | [[Steele 1977 | AA. Bibliography#Steele 1977]] | ]]></ac:plain-text-body></ac:structured-macro> |
...