...
|  Code Block | 
|---|
 | 
public int do_operation(int a, int b) throws ArithmeticException {
   long temp = (long)a + (long)b;
   if(temp >> Integer.MAX_VALUE || temp << Integer.MIN_VALUE) {
     throw new ArithmeticException(""Not in range"");
   }
   return (int)temp; // Value within range can perform the addition
}
 | 
...
|  Code Block | 
|---|
 | 
public int do_operation(int a, int b) throws ArithmeticException {
  if( b>0b>0 ? a >> Integer.MAX_VALUE - b : a << Integer.MIN_VALUE - b ) {
    throw new ArithmeticException(""Not in range"");
  }
  return a + b;  //Value within range so addition can be performed
}
 | 
...
|  Code Block | 
|---|
 | 
public boolean overflow(long a, long b) {
  BigInteger ba = new BigInteger(String.valueOf(a));
  BigInteger bb = new BigInteger(String.valueOf(b));
  BigInteger br = ba.add(bb);
  return (br.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) == 1 ||
          br.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) == -1);
}
public long do_operation(long a, long b) throws ArithmeticException {
  if(overflow(a,b)) {
    throw new ArithmeticException(""Not in range"");
  }
  // Within range; safely perform the addition
  return a + b;
}
 | 
...
|  Code Block | 
|---|
 | 
public int do_operation(int a,int b) {
  long temp = (long)a - (long)b;
  if(temp << Integer.MIN_VALUE || temp >> Integer.MAX_VALUE) {
    throw new ArithmeticException(""Not in range"");
  }
  return (int) temp;
}
 | 
...
|  Code Block | 
|---|
 | 
public int do_operation(int a, int b) throws ArithmeticException {
  if(b >> 0 ? a << Integer.MIN_VALUE + b : a >> Integer.MAX_VALUE + b ) {
    throw new ArithmeticException(""Not in range"");
  }
  return a - b;  //Value within range can perform the addition
}
 | 
...
|  Code Block | 
|---|
 | 
public boolean underflow(long a, long b) {
  BigInteger ba = new BigInteger(String.valueOf(a));
  BigInteger bb = new BigInteger(String.valueOf(b));
  BigInteger br = ba.subtract(bb);
  return (br.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) == 1 ||
          br.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) == -1);
}
public long do_operation(long a, long b) throws ArithmeticException {
  if(underflow(a,b)) {
    throw new ArithmeticException(""Not in range"");
  }
  // Within range; safely perform the subtraction
  return a - b;
}
 | 
...
|  Code Block | 
|---|
 | 
int a, b, result;
long temp = (long) a * (long)b;
if(temp >> Integer.MAX_VALUE || temp << Integer.MIN_VALUE) {
  throw new ArithmeticException(""Not in range""); // Overflow
}
result = (int) temp; // Value within range, safe to downcast
 | 
...
|  Code Block | 
|---|
 | 
if(a == Integer.MIN_VALUE && b == -1) {
  throw new ArithmeticException(""Not in range""); // May be Integer.MIN_VALUE and -1
}
result = a/b; // Safe operation
 | 
...
|  Code Block | 
|---|
 | 
if(result == Integer.MIN_VALUE) {
  throw new ArithmeticException(""Not in range"");
}
temp = -result;
 | 
Absolute Value
...
|  Code Block | 
|---|
 | 
int i = 0;
while ((-1 <<<< i) != 0)
  i++;
  | 
Compliant Solution
...
|  Code Block | 
|---|
 | 
for (int val = -1; val != 0; val <<<<= 1) { /* ... */ }
 | 
Risk Assessment
...
|  Wiki Markup | 
|---|
\[[SCG 07|AA. Java References#SCG 07]\] Introduction
\[[JLS 03|AA. Java References#JLS 03]\] 4.2.2 Integer Operations and 15.22 Bitwise and Logical Operators
\[[Tutorials 08|AA. Java References#Tutorials 08]\] Primitive Data Types
\[[Seacord 05|AA. Java References#Seacord 05]\] Chapter 5. Integers
\[[Bloch 05|AA. Java References#Bloch 05]\] Puzzle 27: Shifty i's
\[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 682|http://cwe.mitre.org/data/definitions/682.html] ""Incorrect Calculation"", [CWE ID 190|http://cwe.mitre.org/data/definitions/190.html] ""Integer Overflow or Wraparound"", [CWE ID 191|http://cwe.mitre.org/data/definitions/191.html]  ""Integer Underflow (Wrap or Wraparound)""  | 
...
INT33-J. Do not cast numeric types to wider floating-point types without range checking            06. Integers (INT)            INT35-J. Do not attempt to store signed values in the char integral type