-4

I have a c# application in which i have this code :

 public static void Main()
        {
            int i = 2147483647;
            int j = i+1;
            Console.WriteLine(j);
            Console.ReadKey();
        }

The result is : -2147483648

I know that every integer must be < 2147483648. So

  • Why I don't have a compilation or runtime error? like in this exemple

img

  • What is the reason of the negative sign?

thanks

Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191

4 Answers4

2

As Christos says, the negative sign comes from integer overflow. The reason you do net get an error is because the compiler does not evaluate expressions for overflowing values.

 0111 1111 1111 1111 1111 1111 1111 1111 2^31-1
+0000 0000 0000 0000 0000 0000 0000 0001 1
=1000 0000 0000 0000 0000 0000 0000 0000 -2^31

The reason for this is that the leftmost bit is the sign bit, it determines whether the int is positive or negative. 0 is positive, 1 is negative. If you add one to the largest possible number, you essentially change the sign bit and get the smallest representable number. The reason for this is that integers use two's complement storage

To check if the value overflows, do:

int j = checked(i + 1);
ThreeFx
  • 7,250
  • 1
  • 27
  • 51
2

The compiler defaults to unchecked arithmetic; you have simply overflown and looped around, thanks to two's-complement storage.

This fails at runtime:

public static void Main()
{
    int i = 2147483647;
    int j = checked((int)(i + 1)); // <==== note "checked"
    Console.WriteLine(j);
    Console.ReadKey();
}

This can also be enabled globally as a compiler-switch.

Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

What is the reason of the negative sign?

You have a negative sign, because you have exceeded the maximum integer value and the next integer is the lowest integer that can be represented.

Why I don't have a compilation or runtime error?

You don't have a complilation error, because this is not an error. Also, this is not a runtime error. You just add one to the i in the runtime. Since the value of i is the maximum integer value that can be stored in a variable of type int and since the circular nature of the integers in programming, you will get the lowest integer that can be stored in a variable of type int.

(A variable of type int can store 32-bit integers).

Furthermore, by default you in C# integer operation don't throw exceptions upon overflow. You could alter this either from project settings or using a checked statement, as it is already have been pointed out here.

Community
  • 1
  • 1
Christos
  • 53,228
  • 8
  • 76
  • 108
1

Why I don't have a compilation or runtime error?

Because compiler can determine that you have assigned a larger than int.MaxValue value to the variable. Since it is hard coded. But for i+1 compiler can't execute the code to determine that the result of this calculation would be greater than int.MaxValue

What is the reason of the negative sign?

It is because of integer overflow.

See: checked (C# Reference)

By default, an expression that contains only constant values causes a compiler error if the expression produces a value that is outside the range of the destination type. If the expression contains one or more non-constant values, the compiler does not detect the overflow.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • In runtime , why I don't have an error?? for the compilation it seems clear – Lamloumi Afif Jul 15 '14 at 15:08
  • @Lamloumi, at runtime you will get an exception `Arithmetic operation resulted in an overflow.`. Errors are at compile time. *(If you have checked keyword)* – Habib Jul 15 '14 at 15:13