1286608618

The title is the remainder for evaluating the numerical sequence 9876543210 as an Integer in Java.  This number lies outsides the bounds of Integer.MAX_VALUE for Java; it’s an overflow error.

1286608618 is the overflow remainder for casting 9876543210 as an Integer in Java.

I ran across this error in some code I was working on that had credit card type account numbers; which are generally 16 digit numbers.  The program in question got the *same* account number from one table as a string, another database as an Integer, and a third table as a BigDecimal.  In the code there were also methods that did conversions  of this number to long and to int.  Normally values like this are stored as strings but I have no comment about the design of this software and database where such things could occur . . .

When the value 1286608618 came back as a value during a unit test, I thought it odd and instead of just taking it as incorrect I researched it.  Since a lot of people use 9876543210 as a test value, perhaps a lot of people have seen this.

There are a few interesting things about this error:

  1. Java doesn’t complain when the bad number gets assigned to Integer.
  2. Java just mindlessly returns the overflow value with no error.
  3. Java makes a few trips through its Integer range until it gets a number that fits within the range.  In the case of 9876543210, it is over 2 times the value of the range, to Java makes those trips through to get to 1286608618.

I wrote a little Groovy code to play around with this:

//Here's the integer and it's overflow
Integer i = 9876543210
println "overflow error for 9876543210: " + i

//Integer max value
BigInteger maxInt = 2**31 - 1
println "calculated max integer: \t" + maxInt
println "Integer.MAX_VALUE: \t\t" + Integer.MAX_VALUE

//Integer min value
BigInteger minInt = -2**31
println "calculated min integer: \t" + minInt
println "Integer.MIN_VALUE: \t\t" + Integer.MIN_VALUE

//get the Integer range
BigInteger range = maxInt  + 1 - minInt
println "range: \t\t\t" + range

//two trips around the raspberry bush calculation
BigInteger big = 9876543210
println "big: \t\t\t" + big
println "overflow remainder: \t\t" + (big - (2*range))

Here’s the outcome:

Well see here, I know thinking about this bakes my bean a little bit too.  There are number shifts and all kinds of thing going on. If you have underflow the same kind of logic applies as well.

I don’t understand why it just doesn’t error — what use is that remainder number at all?  Have no idea.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>