Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Compute modulus division by a power-of-2-number in C#
Computing modulus division by a power-of-2 number in C# can be optimized using bitwise operations. When the divisor is a power of 2 (like 2, 4, 8, 16, etc.), we can use the bitwise AND operation with (divisor - 1) instead of the traditional modulus operator for better performance.
This optimization works because powers of 2 in binary have only one bit set, and subtracting 1 creates a mask that isolates the relevant lower bits.
Syntax
Following is the syntax for computing modulus with a power-of-2 divisor −
result = dividend & (divisor - 1);
This is equivalent to −
result = dividend % divisor; // when divisor is power of 2
How It Works
When a number is a power of 2, it has only one bit set in its binary representation. Subtracting 1 from it creates a mask with all lower bits set to 1.
Using Bitwise AND for Modulus
Example with Power of 2 Divisor
using System;
class Demo {
static uint BitwiseModulus(uint dividend, uint divisor) {
return (dividend & (divisor - 1));
}
static public void Main() {
uint a = 9;
uint b = 8; // power of 2
Console.WriteLine("Using bitwise AND:");
Console.WriteLine(a + " modulus " + b + " = " + BitwiseModulus(a, b));
Console.WriteLine("Using traditional modulus:");
Console.WriteLine(a + " modulus " + b + " = " + (a % b));
}
}
The output of the above code is −
Using bitwise AND: 9 modulus 8 = 1 Using traditional modulus: 9 modulus 8 = 1
Example with Multiple Power-of-2 Values
using System;
class PowerOfTwoModulus {
static void TestModulus(uint dividend, uint divisor) {
uint bitwiseResult = dividend & (divisor - 1);
uint normalResult = dividend % divisor;
Console.WriteLine($"{dividend} % {divisor} = {bitwiseResult} (bitwise) | {normalResult} (normal)");
}
static public void Main() {
Console.WriteLine("Modulus with power-of-2 divisors:");
TestModulus(15, 4); // 15 % 4
TestModulus(23, 8); // 23 % 8
TestModulus(100, 16); // 100 % 16
TestModulus(7, 2); // 7 % 2
}
}
The output of the above code is −
Modulus with power-of-2 divisors: 15 % 4 = 3 (bitwise) | 3 (normal) 23 % 8 = 7 (bitwise) | 7 (normal) 100 % 16 = 4 (bitwise) | 4 (normal) 7 % 2 = 1 (bitwise) | 1 (normal)
Performance Comparison
| Operation | Performance | Use Case |
|---|---|---|
| Bitwise AND (&) | Faster | Divisor is power of 2 |
| Modulus (%) | Slower | Any divisor value |
| Compiler optimization | May auto-convert % to & | Modern C# compilers |
Key Rules
-
This optimization only works when the divisor is a power of 2 (2, 4, 8, 16, 32, etc.).
-
The formula
dividend & (divisor - 1)gives the same result asdividend % divisor. -
Modern C# compilers often perform this optimization automatically.
-
Use this technique in performance-critical code where every cycle counts.
Conclusion
Computing modulus division by power-of-2 numbers using bitwise AND operation (dividend & (divisor - 1)) provides better performance than the traditional modulus operator. This technique leverages the binary representation of power-of-2 numbers to create efficient bit masks for remainder calculations.
