Avoid performing bitwise and arithmetic operations on the same data. In particular, bitwise operations are frequently performed on arithmetic values as a form of premature optimization. Bitwise operators include the unary operator ~ and the binary operators <<
, >>
, &
, ^, and |
. Although such operations are valid and will compile, they can reduce code readability. Declaring a variable as containing a numeric value or a bitmap makes the programmer's intentions clearer and the code more maintainable.
Bitmapped types may be defined to further separate bit collections from numeric types. This may make it easier to verify that bitwise operations are only performed on variables that represent bitmaps.
Code Block |
---|
typedef uint32_t bitmap32_t;
bitmap32_t x = 0x000007f3;
x = (x << 2) | 3; /* shifts in two 1-bits from the right */
|
The typedef
name uintN_t
designates an unsigned integer type with width N
. Consequently, uint32_t
denotes an unsigned integer type with a width of exactly 32 bits. Bitmaps should be declared as unsigned. See recommendation INT13-C. Use bitwise operators only on unsigned operands.
Left- and right-shift operators are often employed to multiply or divide a number by a power of 2. However, using shift operators to represent multiplication or division is an optimization that renders the code less portable and less readable. Furthermore, most compilers routinely will the JVM can optimize multiplications and divisions by constant powers of 2 with bit-shift operations, and they are more familiar with the implementation details of the current platform.
...
In this noncompliant code example, both bit manipulation and arithmetic manipulation are performed on the integer x
. The result is a (prematurely) optimized statement that assigns 5x + 1
to x
for implementations, where integers are represented as two's complement values.
Code Block | ||
---|---|---|
| ||
unsigned int x = 50;
x += (x << 2) + 1;
|
...
Compliant Solution (Left Shift)
In this compliant solution, the assignment statement is modified to reflect the arithmetic nature of x
, resulting in a clearer indication of the programmer's intentions.
Code Block | ||
---|---|---|
| ||
unsigned int x = 50;
x = 5 * x + 1;
|
A reviewer may now recognize that the operation should also be checked for wrappingoverflow. This might not have been apparent in the original, noncompliant code example. For more information, see INT00-J. Ensure that integer operations do not result in overflow.
Noncompliant Code Example (Right Shift)
...
Code Block | ||
---|---|---|
| ||
int x = -50;
x = x + 2;
x >>= 2;
|
Although this code is likely to will perform the division correctly, it is not guaranteed to. If x
has a signed type and a negative value, the operation is implementation defined and can be implemented as either an arithmetic shift or a logical shift. In the event of a logical shift, if the integer is represented in either one's complement or two's complement form, the most significant bit (which controls the sign for both representations) will be set to zero. This will cause a once negative number to become a possibly very large, positive number. For more details, see recommendation INT13-C. Use bitwise operators only on unsigned operands.For example, if the internal representation of x
is 0xFFFF FFCE
(two's complement), an arithmetic shift results in 0xFFFF FFF3
(—13 in two's complement), while a logical shift results in 0x3FFF FFF3
(1,073,741,811 in two's complement).less readable and maintainable than actually making the division explicit.
Compliant Solution (Right Shift)
...
Code Block | ||
---|---|---|
| ||
int x = -50;
x += 2
x /= 4;
|
The resulting value is now more likely to be consistent with the programmer's expectations.
...
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
INT14 INT03-C J | medium | unlikely | medium | P4 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
|
|
|
| ||||||
|
|
|
|
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
Related Guidelines
CERT C Secure Coding Standard: INT14-C. Avoid performing bitwise and arithmetic operations on the same data
CERT C++ Secure Coding Standard: INT14-CPP. Avoid performing bitwise and arithmetic operations on the same data
ISO/IEC 9899:1999 Section 6.2.6.2, "Integer types"
ISO/IEC TR 24772 "STR Bit Representations"
MISRA Rules 6.4 and 6.5
Bibliography
Wiki Markup |
---|
\[[Steele 1977|AA. Bibliography#Steele 77]\] |
...