Compute modulus division by a power-of-2-number
Compute n modulo d without division(/) and modulo(%) operators, where d is a power of 2 number.
Let ith bit from right is set in d. For getting n modulus d, we just need to return 0 to i-1 (from right) bits of n as they are and other bits as 0.
For example if n = 6 (00..110) and d = 4(00..100). Last set bit in d is at position 3 (from right side). So we need to return last two bits of n as they are and other bits as 0, i.e., 00..010.
Now doing it is so easy, guess it….
Yes, you have guessing it right. See the below program.
C++
#include<stdio.h> // This function will return n % d. // d must be one of: 1, 2, 4, 8, 16, 32, … unsigned int getModulo(unsigned int n, unsigned int d) { return ( n & (d - 1) ); } // Driver Code int main() { unsigned int n = 6; // d must be a power of 2 unsigned int d = 4; printf("%u moduo %u is %u", n, d, getModulo(n, d)); getchar(); return 0; } |
chevron_right
filter_none
Java
// Java code for Compute modulus division by // a power-of-2-number class GFG { // This function will return n % d. // d must be one of: 1, 2, 4, 8, 16, 32, static int getModulo(int n, int d) { return ( n & (d-1) ); } // Driver Code public static void main(String[] args) { int n = 6; /*d must be a power of 2*/ int d = 4; System.out.println(n+" moduo " + d + " is " + getModulo(n, d)); } } // This code is contributed // by Smitha Dinesh Semwal. |
chevron_right
filter_none
Python3
# Python code to demonstrate # modulus division by power of 2 # This function will # return n % d. # d must be one of: # 1, 2, 4, 8, 16, 32, … def getModulo(n, d): return ( n & (d-1) ) # Driver program to # test above function n = 6 #d must be a power of 2 d = 4 print(n,"moduo",d,"is", getModulo(n, d)) # This code is contributed by # Smitha Dinesh Semwal |
chevron_right
filter_none
C#
// C# code for Compute modulus // division by a power-of-2-number using System; class GFG { // This function will return n % d. // d must be one of: 1, 2, 4, 8, 16, 32, … static uint getModulo( uint n, uint d) { return ( n & (d-1) ); } // Driver code static public void Main () { uint n = 6; uint d = 4; /*d must be a power of 2*/ Console.WriteLine( n + " moduo " + d + " is " + getModulo(n, d)); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // This function will return n % d. // d must be one of: 1, 2, 4, 8, 16, 32, … function getModulo($n, $d) { return ( $n & ($d - 1) ); } // Driver Code $n = 6; // d must be a power of 2 $d = 4; echo $n ," moduo"," ", $d, " is ", " ",getModulo($n, $d); // This code is contributed by vt_m. ?> |
chevron_right
filter_none
References:
http://graphics.stanford.edu/~seander/bithacks.html#ModulusDivisionEasy
Please write comments if you find any bug in the above program/algorithm or other ways to solve the same problem.
Recommended Posts:
- Compute the minimum or maximum of two integers without branching
- Compute the integer absolute value (abs) without branching
- Optimization Techniques | Set 1 (Modulus)
- Calculate 7n/8 without using division and multiplication operators
- Cyclic Redundancy Check and Modulo-2 Division
- Compute the parity of a number using XOR and table look-up
- Divide two integers without using multiplication, division and mod operator
- Fast average of two numbers without division
- Check a number is odd or even without modulus operator
- Count number of steps to cover a distance if steps can be taken in powers of 2
- Queries to find the count of integers in a range that contain the given pattern
- Count of N-bit binary numbers without leading zeros
- Maximum distance between two 1's in Binary representation of N
- Maximize the Expression | Bit Manipulation
Improved By : vt_m



