Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Left- and right-shift operators are often employed to multiply or divide a number by a power of two. This compromises code readability and portability for the sake of often-illusory speed gains. The Java Virtual Machine (JVM) usually makes such optimizations automatically, and, unlike a programmer, the JVM can optimize for the implementation details of the current platform. This noncompliant code example includes both bit bitwise and arithmetic manipulations of the integer x that conceptually contains a numeric value. The result is a prematurely optimized statement that assigns the value 5x + 1 to x, which is what the programmer intended to express.

...

This example is noncompliant because the actual data receives has both bitwise and arithmetic operations performed on it, even though the operations are segregated onto performed on different variables.

Compliant Solution (Left Shift)

...

In this compliant solution, the right shift is correctly replaced by division.

Code Block
bgColor#ccccff
int x = -50;
x /= 4;

...

Nevertheless, as a matter of style, we recommend replacing it is preferable to replace such constant expressions with the equivalent hexadecimal constants.

...

Risk Assessment

Performing bit bitwise manipulation and arithmetic operations on the same variable obscures the programmer's intentions and reduces readability. This, in turn, makes it more difficult for a security auditor or maintainer to determine which checks must be performed to eliminate security flaws and ensure data integrity. For instance, overflow checks are critical for numeric types that undergo arithmetic operations, but less critical for numeric types that undergo bitwise operations.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="dfd6ea5742c1b118-e20273e9-4311468a-90a6b11a-f72224f5a0a6fe141f9af2f1"><ac:plain-text-body><![CDATA[

[[Steele 1977

AA. Bibliography#Steele 1977]]

]]></ac:plain-text-body></ac:structured-macro>

...