Compute modulus division by a power-of-2-number - GeeksforGeeks

14 Mar, 2022
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++
C
Java
Python3
C#
PHP
Javascript
#include<iostream>using namespace std;// 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 Codeint main(){  unsigned int n = 6;   // d must be a power of 2  unsigned int d = 4;  cout<< n <<" modulo "<<d <<" is "<< getModulo(n, d);   getchar();  return 0;}     // this code is contributed by shivanisinghss2110
#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 Codeint main(){unsigned int n = 6; // d must be a power of 2unsigned int d = 4;printf("%u modulo %u is %u", n, d, getModulo(n, d)); getchar();return 0;}    
// Java code for Compute modulus division by// a power-of-2-numberclass 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+" modulo " + d +                    " is " + getModulo(n, d));    }} // This code is contributed// by Smitha Dinesh Semwal.
# 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 functionn = 6 #d must be a power of 2d = 4print(n,"modulo",d,"is",      getModulo(n, d)) # This code is contributed by# Smitha Dinesh Semwal
// C# code for Compute modulus// division by a power-of-2-numberusing 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 codestatic public void Main ()   {    uint n = 6;    uint d = 4; /*d must be a power of 2*/     Console.WriteLine( n + " modulo " + d +                " is " + getModulo(n, d));         }}// This code is contributed by vt_m.
<?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 ," modulo"," ", $d, " is ",         " ",getModulo($n, $d);     // This code is contributed by vt_m.?>
<script>// 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 = 4;  document.write(n  +" modulo "+ d + " is "+ getModulo(n, d));   // This code is contributed by simranarora5sos</script>
6 modulo 4 is 2
Time Complexity: O(1)
Auxiliary Space: O(1)
https://www.youtube.com/watch?v=fSjW-wDghTs 
References: http://graphics.stanford.edu/~seander/bithacks.html#ModulusDivisionEasyPlease write comments if you find any bug in the above program/algorithm or other ways to solve the same problem.  
 
vt_m
simranarora5sos
kavania2002
souravmahato348
rishavmahato348
shivanisinghss2110
varshagumber28
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Bit Fields in C
Bits manipulation (Important tactics)
Find the element that appears once
1's and 2's complement of a Binary Number
Divide two integers without using multiplication, division and mod operator
C++ bitset and its application
Check whether K-th bit is set or not
Write an Efficient C Program to Reverse Bits of a Number
Find most significant set bit of a number