Integer Arithmetic

Obviously, the arithmetic operators can operate on integral values, including int, uint, long, and ulong. A few examples are shown here:

int a = 6;int b = 7;int mul = a * b;     // 42int add = mul + a;   // 48int div = add / b;   // 6int sub = mul - div; // 36int rem = sub % b;   // 1

All of this should be self-explanatory, except perhaps for the division and remainder operations. Integral division produces an integral result, rounding toward zero. For example, 3 / 2 will produce 1, and -3 / 2 will produce -1. If you want fractional results, at least one fractional number will have to be involved in the operation.

For the remainder operator, an invariant holds in relation to the subtraction, multiplication, and addition operators. ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.