Category Archives: calculations

Define and use bit masks in C#

The example Understand bit masks in C# explains how to use bit masks. To define a bit mask, simply create an enum and give it the Flags attribute as in the following code. [Flags] private enum BitmaskEnum { Value1 = … Continue reading

Posted in algorithms, calculations, mathematics, variables | Tagged , , , , , , , , , , , , , | Leave a comment

Understand bit masks in C#

Some values, including some properties defined by the .NET Framework, are bit masks. That means each bit in a value means something. For example, the AnchorStyles enumeration that determines how controls are anchored in their parents defines four values: Top, … Continue reading

Posted in algorithms, calculations, mathematics, variables | Tagged , , , , , , , , , , , | Leave a comment

Use bit operations in C#

C# defines several operators that perform bit operations. As you may be able to guess from the names, these operators manipulate the bits in an integer value. They operate on the value’s bits separately so they are sometimes called “bitwise … Continue reading

Posted in calculations, mathematics, variables | Tagged , , , , , , , , , , , , , | Leave a comment

Compare speeds of arithmetic operations in C#

This example compares the speeds of arithmetic operations with different data types. There’s a big difference between the speeds of operations using the decimal data type and the other types, so performing a loop a certain number of time is … Continue reading

Posted in algorithms, calculations, mathematics | Tagged , , , , , , , , , , , , , , , , | Leave a comment

Calculate credit payments in C#

For each month the program: Calculates the payment first. This is either a percent of the balance or the minimum amount, whichever is greater. Calculates interest after the payment is calculated but before it is subtracted from the balance. Adds … Continue reading

Posted in algorithms, calculations, finance, mathematics | Tagged , , , , , , , , , , , , , | Leave a comment

Make a stacked graph showing compound interest in C#

The example Show compound interest graphically in C# displays a graph showing contributions, compound interest, and total balance over time for a monthly investment strategy. This example may provide a better representation than that one. It shows the total contributions … Continue reading

Posted in algorithms, calculations, graphics, mathematics | Tagged , , , , , , , , , , , , | Leave a comment

Show compound interest graphically in C#

This is a more graphic version of the example Calculate the value of a monthly investment in C#. Instead of adding values to a ListView control, this example saves points in three lists of points: Balance, Contributions, and Interest. It … Continue reading

Posted in algorithms, calculations, graphics, mathematics | Tagged , , , , , , , , , , , | 1 Comment

Calculate the value of a monthly investment in C#

The magic of an investment with compound interest is that, over time, you get interest on the interest. For each month this program calculates the interest on the account balance. It then adds the interest and a monthly contribution to … Continue reading

Posted in algorithms, calculations, finance, mathematics | Tagged , , , , , , , , , , , | 1 Comment

Make generic Min and Max methods in C#

This example shows how you can make generic Min and Max methods to find the minimum and maximum values in a sequence of parameters. The Math namespace’s Min and Max methods are very useful, but they have two big drawbacks. … Continue reading

Posted in algorithms, arrays, calculations, generic, methods | Tagged , , , , , , , , , , , , , , | Leave a comment

See how much time a checked block takes in C#

The post Control overflow behavior with checked and unchecked in C# explains how to use checked blocks to watch for integer overflow. If you don’t use checked, the program may encounter integer overflow errors without you knowing it. It seems … Continue reading

Posted in calculations, mathematics, performance, syntax | Tagged , , , , , , , , , , , , , , , | Leave a comment