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<iostream>
usingnamespacestd;
// This function will return n % d.
// d must be one of: 1, 2, 4, 8, 16, 32, …
unsigned intgetModulo(unsigned intn,
unsigned intd)
{
return( n & (d - 1) );
}
// Driver Code
intmain()
{
unsigned intn = 6;
// d must be a power of 2
unsigned intd = 4;
cout<< n <<" modulo "<<d <<" is "<< getModulo(n, d);
getchar();
return0;
}
// this code is contributed by shivanisinghss2110
C
#include<stdio.h>
// This function will return n % d.
// d must be one of: 1, 2, 4, 8, 16, 32, …
unsigned intgetModulo(unsigned intn,
unsigned intd)
{
return( n & (d - 1) );
}
// Driver Code
intmain()
{
unsigned intn = 6;
// d must be a power of 2
unsigned intd = 4;
printf("%u modulo %u is %u", n, d, getModulo(n, d));
getchar();
return0;
}
Java
// Java code for Compute modulus division by
// a power-of-2-number
classGFG {
// This function will return n % d.
// d must be one of: 1, 2, 4, 8, 16, 32,
staticintgetModulo(intn, intd)
{
return( n & (d-1) );
}
// Driver Code
publicstaticvoidmain(String[] args)
{
intn = 6;
/*d must be a power of 2*/
intd = 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, …
defgetModulo(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
C#
// C# code for Compute modulus
// division by a power-of-2-number
usingSystem;
classGFG {
// This function will return n % d.
// d must be one of: 1, 2, 4, 8, 16, 32, …
staticuintgetModulo( uintn, uintd)
{
return( n & (d-1) );
}
// Driver code
staticpublicvoidMain ()
{
uintn = 6;
uintd = 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, …
functiongetModulo($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.
?>
Javascript
<script>
// This function will return n % d.
// d must be one of: 1, 2, 4, 8, 16, 32, …
functiongetModulo(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>
Output
6 modulo 4 is 2
Time Complexity: O(1)
As we are doing single operation which takes constant time.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy