Compute modulus division by a power-of-2-number in C#


We have taken the number as the following −

uint a = 9;
uint b = 8;

Above, a is a divisor and b is dividend.

To compute modulus division.

Example

 Live Demo

using System;
class Demo {
   static uint display( uint a, uint b) {
      return ( a & (b-1) );
   }
   static public void Main () {
      uint a = 9;
      uint b = 6;
      Console.WriteLine( a + " modulus " + b + " = " + display(a, b));
   }
}

Output

9 modulus 6 = 1

Updated on: 23-Jun-2020

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements