O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required