HomeSearch | ## C# Modulo Operator: Get Remainder From DivisionUse the modulo division operator to get remainders from dividing numbers. | |

## Modulo.This operator gets a remainder. It provides a way to execute code once every several iterations of a loop. It uses the percentage sign character in the lexical syntax. | ||

## Modulo, notes.Modulo has some unique properties. As with all low-level operations, it has a specific cost. We analyze modulo division in the C# language.DivideOdd, Even | ||

## Example.Modulo division is expressed with the percentage sign. It is implemented with the rem instruction in the intermediate language. Rem takes the top 2 values on the evaluation stack.
| C# program that uses modulo operator
using System;
class Program
{
static void Main()
{
//
// When 1000 is divided by 90, the remainder is 10.
//
Console.WriteLine(1000 % 90);
//
// When 100 is divided by 90, the remainder is also 10.
//
Console.WriteLine(100 % 90);
//
// When 81 is divided by 80, the remainder is 1.
//
Console.WriteLine(81 % 80);
//
// When 1 is divided by 1, the remainder is zero.
//
Console.WriteLine(1 % 1);
}
}
Output
10
10
1
0 | |

## Notes, above program.The program shows the remainders of the divisions of the 2 integers at each step. The runtime never performs modulo divisions here. The C# compiler does.
| ||

## Notes, continued.If you use modulo on the same 2 operands, you receive 0 because there is no remainder. If you use modulo by 0, you will get a compile error or a runtime exception.DivideByZeroExceptionCompile-Time Error | ||

## Example 2.You can apply modulo in a loop to achieve an interval or step effect. If you use a modulo operation on the loop index variable, you can execute code at an interval.
| C# program that uses modulo division in loop
using System;
class Program
{
static void Main()
{
//
// Prints every tenth number from 0 to 200.
// Includes the first iteration.
//
for (int i = 0; i < 200; i++)
{
if ((i % 10) == 0)
{
Console.WriteLine(i);
}
}
}
}
Output
0
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190 | |

## Notes, if.Often, modulo divisions are performed in ifs and used in control flow. The three numbers in the condition in the if-statement can have any values, but we cannot divide by 0. | ||

## Discussion.Modulo has several common uses in programs. You can use modulo division in loops to only execute code every several iterations. This can improve real code.
| ||

## Performance.Modulo is slower than other arithmetic operators such as increment and decrement or even multiply. This is a hardware limitation on computers.
| ||

## Performance, table.The time required for modulo division depends on hardware and other factors. Some analysis from Microsoft is helpful. This page provides a table listing times required.Writing Faster Managed Code: Microsoft Docs | Estimated costs of instructions:
Add: 1 ns
Subtract: 1 ns
Multiply: 2.7 ns
Divide: 35.9 ns | |

## Performance, loops.You may rarely have a modulo division in a hot path and this can cause a loss of performance. This will almost always occur in a loop body or in a recursive method.
| ||

## A summary.The modulo operator is often useful. This is implemented in the CLI as a rem instruction. The C# compiler calculates modulo divisions of constants at compile-time. | ||

## Modulo divisionreturns the remainder of the 2 operands. We use the "percent" symbol for modulo in the C# language. This is a powerful operator, but it has its nuances. | ||

Home Dot Net Perls | © 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com. |