What Happens When You Increment an Integer Beyond Its Max Value

What happens when you increment an integer beyond its max value?

From the Java Language Specification section on integer operations:

The built-in integer operators do not
indicate overflow or underflow in any
way.

The results are specified by the language and independent of the JVM version: Integer.MAX_VALUE + 1 == Integer.MIN_VALUE and Integer.MIN_VALUE - 1 == Integer.MAX_VALUE. The same goes for the other integer types.

The atomic integer objects (AtomicInteger, AtomicLong, etc.) use the normal integer operators internally, so getAndDecrement(), etc. behave this way as well.

Incrementing an integer value beyond its integer limit - C#

Similar to the behaviour in some implentations of C where an int just wraps around from INT_MAX to INT_MIN ( though it's actually undefined behaviour according to the ISO standard), C# also wraps. Testing it in VS2008 with:

int x = 2147483647;
if (x+1 < x) {
MessageBox.Show("It wrapped...");
}

will result in the message box appering.

If your hugetValue is greater than the maximum int value, then your loop will run forever because of this.

For example, if it's 2147483648, just as you think you're getting close to it, the int wraps around from 2147483647 back to -2147483648 and the loop just keeps on going.

does the incrementation of unsigned int cause undefined behavior when the variable reach the Max

a= a+1; //is it allowed to increment "a" when "a" reach the Max ? 

Yes, unsigned integers never overflow (this is C terminology). So UINT_MAX + 1 is defined behavior and is evaluated to 0.

(C99, 6.2.5p9) "A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type."

Why does the loop iterate beyond Integer.MAX_VALUE?

Look at

 i <= Integer.MAX_VALUE

This is always true. If i = Integer.MAX_VALUE+1 then it will overflow, and become negative.

Do this:

 System.out.println(Integer.MAX_VALUE+1)

Weird Result Incrementing a Short Beyond its Maximum Value

Good question! It made me think about things I haven't thought about in a long while and I had to brush up on a couple of concepts. Thanks for helping me knock the rust off my brain.

For me this type of question is best visualized in binary (for reasons that will quickly become apparent):

Your original number (forgive the leading zeroes; I like groups of 4):

0001 1110 0010 0100 0000

A short, however, is a 16-bit signed two's complement integer according to the Java Language Specification (JLS) section 4.2. Assigning the integer value 123456 to a short is known as a "narrowing primitive conversion" which is covered in JLS 5.1.3. Specifically, a "narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T."

Discarding all but the lowest 16 bits leaves us with:

1110 0010 0100 0000

In an unsigned integer this value is 57,290, however the short integer is a signed two's complement integer. The 1 in the leftmost digit indicates a negative number; to get the value of the number you must invert the bits and add 1:

Original:

1110 0010 0100 0000

Invert the bits:

0001 1101 1011 1111

Add 1:

0001 1101 1100 0000

Convert that to decimal and add the negative sign to get -7,616.

Thanks again for asking the question. It's okay to not know something so keep asking and learning. I had fun answering...I like diving into the JLS, crazy, I know!

How can I increment a variable without exceeding a maximum value?

I would just do this. It basically takes the minimum between 100 (the max health) and what the health would be with 15 extra points. It ensures that the user's health does not exceed 100.

public void getHealed() {
health = Math.min(health + 15, 100);
}

To ensure that hitpoints do not drop below zero, you can use a similar function: Math.max.

public void takeDamage(int damage) {
if(damage > 0) {
health = Math.max(health - damage, 0);
}
}

AtomicInteger incrementation

It wraps around, due to integer overflow, to Integer.MIN_VALUE:

System.out.println(new AtomicInteger(Integer.MAX_VALUE).incrementAndGet());
System.out.println(Integer.MIN_VALUE);

Output:

-2147483648
-2147483648

What happens when auto_increment on integer column reaches the max_value in databases?

Jim Martin's comment from §3.6.9. "Using AUTO_INCREMENT" of the MySQL documentation:

Just in case there's any question, the AUTO_INCREMENT field /DOES NOT WRAP/. Once you hit the limit for the field size, INSERTs generate an error. (As per Jeremy Cole)

A quick test with MySQL 5.1.45 results in an error of:

ERROR 1467 (HY000): Failed to read auto-increment value from storage engine

You could test for that error on insert and take appropriate action.



Related Topics



Leave a reply



Submit