2.1 Integers advanced notes

In this article

  1. Division
  2. Overflow
    1. Overflow Check Operator
    2. Overflow checking for constant expressions
  3. 8- and 16-Bit Integral Types

Division

  • Division operations on integral types always eliminate the remainder (round toward zero).
  • Dividing by a variable whose value is zero generates a runtime error (a DivideByZeroException):
int a = 2 / 3; // 0
int b = 0;
int c = 5 / b; // throws DivideByZeroException
  • Dividing by the literal or constant 0 generates a compile-time error.

Overflow

At runtime, arithmetic operations on integral types can overflow. By default, this happens silently—no exception is thrown, and the result exhibits “wraparound” behavior, as though the computation were done on a larger integer type and the extra significant bits discarded.

For example, decrementing the minimum possible int value results in the maximum possible int value:

int a = int.MinValue;
a--;
Console.WriteLine (a == int.MaxValue); // True

Overflow Check Operator

The checked operator instructs the runtime to generate an OverflowException rather than overflowing silently when an integral-type expression or statement exceeds the arithmetic limits of that type. The checked operator affects expressions with the ++, −−, +, (binary and unary), *, /, and explicit conversion operators between integral types. Overflow checking incurs a small performance cost.

You can use checked around either an expression or a statement block:

int a = 1000000;
int b = 1000000;
int c = checked (a * b); // Checks just the expression.
checked // Checks all expressions
{ // in statement block.
    c = a * b;
}

You can make arithmetic overflow checking the default for all expressions in a program by selecting the “checked” option at the project level (in Visual Studio, go to Advanced Build Settings).

If you then need to disable overflow checking just for specific expressions or statements, you can do so with the unchecked operator.

For example, the following code will not throw exceptions—even if the project’s
“checked” option is selected:

    int x = int.MaxValue;
    int y = unchecked (x + 1);
    unchecked { int z = x + 1; }

Overflow checking for constant expressions

Regardless of the “checked” project setting, expressions evaluated at compile time
are always overflow-checked—unless you apply the unchecked operator:

    int x = int.MaxValue + 1; // Compile-time error
    int y = unchecked (int.MaxValue + 1); // No errors

8- and 16-Bit Integral Types

The 8- and 16-bit integral types are byte, sbyte, short, and ushort. These types lack their own arithmetic operators, so C# implicitly converts them to larger types as required. This can cause a compile-time error when trying to assign the result back to a small integral type:

short x = 1, y = 1;
short z = x + y; // Compile-time error

In this case, x and y are implicitly converted to int so that the addition can be
performed. This means that the result is also an int, which cannot be implicitly cast
back to a short (because it might cause a loss of data). To make this compile, you
must add an explicit cast:

short z = (short) (x + y); // OK

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s