Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Compute n modulo d without division(/) and modulo(%) operators, where d is a power of 2 number. 

Input: 6 4
Output: 2 
Explanation: As 6%4 = 2

Input: 12 8
Output: 4
Explanation: As 12%8 = 4

Input: 10 2
Output: 0
Explanation:As 10%2 = 0


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 modulo %u is %u", n, d, getModulo(n, d));
 
getchar();
return 0;
}    

                    

C++

#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 Code
int 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

                    

Java

// Java code for Compute modulus division by
// a power-of-2-number
import java.io.*;
public 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+" modulo " + d +
                    " is " + getModulo(n, d));
    }
}
 
// This code is contributed
// by Smitha Dinesh Semwal.

                    

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,"modulo",d,"is",
      getModulo(n, d))
 
# This code is contributed by
# Smitha Dinesh Semwal

                    

Javascript

<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>

                    

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 + " modulo " + d +
                " is " + getModulo(n, d));
     
    }
}
// This code is contributed by vt_m.

                    

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 ," modulo"," ", $d, " is ",
         " ",getModulo($n, $d);
     
// This code is contributed by vt_m.
?>

                    

Output
6 modulo 4 is 2

Time Complexity: O(1), As we are doing single operation which takes constant time.
Auxiliary Space: O(1), As constant extra space is used.
 

Another Approach:

  • Read in the values of n and d from the user.
  • Subtract 1 from d to get a number with all bits set to 1 up to the position of the highest set bit in d. This is called the mask.
  • Perform a bitwise AND of n and the mask to get the remainder. This will give us the last i bits of n where i is the position of the highest set bit in d.
  • Output the result.

C++

#include <iostream>
 
using namespace std;
 
int main()
{
 
    int n, d;
 
    n = 6;
    d = 4;
 
    int mask = d - 1; // mask is a number with all bits set
                      // to 1 up to the position of the
                      // highest set bit in d
 
    int result = n & mask; // bitwise AND of n and mask
                           // gives the remainder
 
    cout << result << endl;
 
    return 0;
}

                    

Java

import java.io.*;
 
 
public class Main {
    public static void main(String[] args) {
        int n, d;
 
        n = 6;
        d = 4;
 
        int mask = d - 1; // mask is a number with all bits set
                          // to 1 up to the position of the
                          // highest set bit in d
 
        int result = n & mask; // bitwise AND of n and mask
                               // gives the remainder
 
        System.out.println(result);
    }
}

                    

Javascript

let n, d;
 
n = 6;
d = 4;
 
let mask = d - 1; // mask is a number with all bits set
                  // to 1 up to the position of the
                  // highest set bit in d
 
let result = n & mask; // bitwise AND of n and mask
                       // gives the remainder
 
document.write(result);

                    

C#

using System;
 
class Program
{
    static void Main(string[] args)
    {
        int n, d;
 
        n = 6;
        d = 4;
 
        int mask = d - 1; // mask is a number with all bits set
                          // to 1 up to the position of the
                          // highest set bit in d
 
        int result = n & mask; // bitwise AND of n and mask
                               // gives the remainder
 
        Console.WriteLine(result);
 
        Console.ReadLine();
    }
}

                    

Python3

n = 6
d = 4
 
mask = d - 1
 
result = n & mask
 
print(result)

                    

Output
2

Time Complexity: O(1)
Auxiliary Space: O(1)

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. 



Last Updated : 19 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads