Compute modulus division by a power of 2 number

Here given code implementation process.

``````/*
C program for
Compute modulus division by a power of 2 number
*/
#include <stdio.h>

void modulusDivision(int a, int b)
{
printf("\n Given a : %d b %d ", a, b);
// Assume b is power of 2
// Case A Using modulus operator
int ans1 = a % b;
printf("\n Using modulus :  (%d  %%  %d) = %d", a, b, ans1);
// Case A Using bitwise operator
int ans2 = a & (b - 1);
printf("\n Using bitwise :  (%d & (%d - 1)) = %d\n", a, b, ans2);
}
int main()
{
int a = 37;
// b is power of 2
int b = 4;
modulusDivision(a, b);
a = 63;
// b is power of 2
b = 8;
modulusDivision(a, b);
return 0;
}``````

Output

`````` Given a : 37 b 4
Using modulus :  (37  %  4) = 1
Using bitwise :  (37 & (4 - 1)) = 1

Given a : 63 b 8
Using modulus :  (63  %  8) = 7
Using bitwise :  (63 & (8 - 1)) = 7``````
``````// Java Program
// Compute modulus division by a power of 2 number
public class Division
{
public void modulusDivision(int a, int b)
{
System.out.print("\n Given a : " +
a + " b : " + b);
// Assume b is power of 2
// Case A Using modulus operator
int ans1 = a % b;
System.out.print("\n Using modulus : (" +
a + " % " + b + ") = " + ans1);
// Case A Using bitwise operator
int ans2 = a & (b - 1);
System.out.println("\n Using bitwise : (" +
a + " & (" + b + " - 1)) = " + ans2);
}
public static void main(String args[])
{
int a = 37;
// b is power of 2
int b = 4;
a = 63;
// b is power of 2
b = 8;
}
}``````

Output

`````` Given a : 37 b : 4
Using modulus : (37 % 4) = 1
Using bitwise : (37 & (4 - 1)) = 1

Given a : 63 b : 8
Using modulus : (63 % 8) = 7
Using bitwise : (63 & (8 - 1)) = 7``````
``````// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Compute modulus division by a power of 2 number
class Division
{
public: void modulusDivision(int a, int b)
{
cout << "\n Given a : " << a << " b : " << b;
// Assume b is power of 2
// Case A Using modulus operator
int ans1 = a % b;
cout << "\n Using modulus : ("
<< a << " % " << b << ") = "
<< ans1;
// Case A Using bitwise operator
int ans2 = a &(b - 1);
cout << "\n Using bitwise : ("
<< a << " &(" << b << " - 1)) = "
<< ans2 << endl;
}
};
int main()
{
int a = 37;
// b is power of 2
int b = 4;
a = 63;
// b is power of 2
b = 8;
return 0;
}``````

Output

`````` Given a : 37 b : 4
Using modulus : (37 % 4) = 1
Using bitwise : (37 &(4 - 1)) = 1

Given a : 63 b : 8
Using modulus : (63 % 8) = 7
Using bitwise : (63 &(8 - 1)) = 7``````
``````// Include namespace system
using System;
// Csharp Program
// Compute modulus division by a power of 2 number
public class Division
{
public void modulusDivision(int a, int b)
{
Console.Write("\n Given a : " + a + " b : " + b);
// Assume b is power of 2
// Case A Using modulus operator
int ans1 = a % b;
Console.Write("\n Using modulus : (" +
a + " % " + b + ") = " + ans1);
// Case A Using bitwise operator
int ans2 = a & (b - 1);
Console.WriteLine("\n Using bitwise : (" +
a + " & (" + b + " - 1)) = " + ans2);
}
public static void Main(String[] args)
{
int a = 37;
// b is power of 2
int b = 4;
a = 63;
// b is power of 2
b = 8;
}
}``````

Output

`````` Given a : 37 b : 4
Using modulus : (37 % 4) = 1
Using bitwise : (37 & (4 - 1)) = 1

Given a : 63 b : 8
Using modulus : (63 % 8) = 7
Using bitwise : (63 & (8 - 1)) = 7``````
``````<?php
// Php Program
// Compute modulus division by a power of 2 number
class Division
{
public	function modulusDivision(\$a, \$b)
{
echo("\n Given a : ".\$a.
" b : ".\$b);
// Assume b is power of 2
// Case A Using modulus operator
\$ans1 = \$a % \$b;
echo("\n Using modulus : (".\$a.
" % ".\$b.
") = ".\$ans1);
// Case A Using bitwise operator
\$ans2 = \$a & (\$b - 1);
echo("\n Using bitwise : (".\$a.
" & (".\$b.
" - 1)) = ".\$ans2.
"\n");
}
}

function main()
{
\$a = 37;
// b is power of 2
\$b = 4;
\$a = 63;
// b is power of 2
\$b = 8;
}
main();``````

Output

`````` Given a : 37 b : 4
Using modulus : (37 % 4) = 1
Using bitwise : (37 & (4 - 1)) = 1

Given a : 63 b : 8
Using modulus : (63 % 8) = 7
Using bitwise : (63 & (8 - 1)) = 7``````
``````package main
import "fmt"
// Go Program
// Compute modulus division by a power of 2 number

func modulusDivision(a, b int) {
fmt.Print("\n Given a : ", a, " b : ", b)
// Assume b is power of 2
// Case A Using modulus operator
var ans1 int = a % b
fmt.Print("\n Using modulus : (", a, " % ", b, ") = ", ans1)
// Case A Using bitwise operator
var ans2 int = a & (b - 1)
fmt.Println("\n Using bitwise : (", a, " & (", b, " - 1)) = ", ans2)
}
func main() {

var a int = 37
// b is power of 2
var b int = 4
modulusDivision(a, b)
a = 63
// b is power of 2
b = 8
modulusDivision(a, b)
}``````

Output

`````` Given a : 37 b : 4
Using modulus : (37 % 4) = 1
Using bitwise : (37 & (4 - 1)) = 1

Given a : 63 b : 8
Using modulus : (63 % 8) = 7
Using bitwise : (63 & (8 - 1)) = 7``````
``````// Node JS Program
// Compute modulus division by a power of 2 number
class Division
{
modulusDivision(a, b)
{
process.stdout.write("\n Given a : " + a + " b : " + b);
// Assume b is power of 2
// Case A Using modulus operator
var ans1 = a % b;
process.stdout.write("\n Using modulus : (" +
a + " % " + b + ") = " + ans1);
// Case A Using bitwise operator
var ans2 = a & (b - 1);
console.log("\n Using bitwise : (" +
a + " & (" + b + " - 1)) = " + ans2);
}
}

function main()
{
var a = 37;
// b is power of 2
var b = 4;
a = 63;
// b is power of 2
b = 8;
}
main();``````

Output

`````` Given a : 37 b : 4
Using modulus : (37 % 4) = 1
Using bitwise : (37 & (4 - 1)) = 1

Given a : 63 b : 8
Using modulus : (63 % 8) = 7
Using bitwise : (63 & (8 - 1)) = 7``````
``````#  Python 3 Program
#  Compute modulus division by a power of 2 number
class Division :
def modulusDivision(self, a, b) :
print("\n Given a : ", a ," b : ", b, end = "")
#  Assume b is power of 2
#  Case A Using modulus operator
ans1 = a % b
print("\n Using modulus : (", a ," % ", b ,") = ", ans1, end = "")
#  Case A Using bitwise operator
ans2 = a & (b - 1)
print("\n Using bitwise : (", a ," & (", b ," - 1)) = ", ans2)

def main() :
a = 37
#  b is power of 2
b = 4
a = 63
#  b is power of 2
b = 8

if __name__ == "__main__": main()``````

Output

`````` Given a :  37  b :  4
Using modulus : ( 37  %  4 ) =  1
Using bitwise : ( 37  & ( 4  - 1)) =  1

Given a :  63  b :  8
Using modulus : ( 63  %  8 ) =  7
Using bitwise : ( 63  & ( 8  - 1)) =  7``````
``````#  Ruby Program
#  Compute modulus division by a power of 2 number
class Division
def modulusDivision(a, b)
print("\n Given a : ", a ," b : ", b)
#  Assume b is power of 2
#  Case A Using modulus operator
ans1 = a % b
print("\n Using modulus : (", a ," % ", b ,") = ", ans1)
#  Case A Using bitwise operator
ans2 = a & (b - 1)
print("\n Using bitwise : (", a ," & (", b ," - 1)) = ", ans2, "\n")
end

end

def main()
a = 37
#  b is power of 2
b = 4
a = 63
#  b is power of 2
b = 8
end

main()``````

Output

`````` Given a : 37 b : 4
Using modulus : (37 % 4) = 1
Using bitwise : (37 & (4 - 1)) = 1

Given a : 63 b : 8
Using modulus : (63 % 8) = 7
Using bitwise : (63 & (8 - 1)) = 7
``````
``````// Scala Program
// Compute modulus division by a power of 2 number
class Division()
{
def modulusDivision(a: Int, b: Int): Unit = {
print("\n Given a : " + a + " b : " + b);
// Assume b is power of 2
// Case A Using modulus operator
var ans1: Int = a % b;
print("\n Using modulus : (" + a + " % " + b + ") = " + ans1);
// Case A Using bitwise operator
var ans2: Int = a & (b - 1);
println("\n Using bitwise : (" + a + " & (" + b + " - 1)) = " + ans2);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Division = new Division();
var a: Int = 37;
// b is power of 2
var b: Int = 4;
a = 63;
// b is power of 2
b = 8;
}
}``````

Output

`````` Given a : 37 b : 4
Using modulus : (37 % 4) = 1
Using bitwise : (37 & (4 - 1)) = 1

Given a : 63 b : 8
Using modulus : (63 % 8) = 7
Using bitwise : (63 & (8 - 1)) = 7``````
``````// Swift 4 Program
// Compute modulus division by a power of 2 number
class Division
{
func modulusDivision(_ a: Int, _ b: Int)
{
print("\n Given a : ", a ," b : ", b, terminator: "");
// Assume b is power of 2
// Case A Using modulus operator
let ans1: Int = a % b;
print("\n Using modulus : (", a ," % ", b ,") = ",
ans1, terminator: "");
// Case A Using bitwise operator
let ans2: Int = a & (b - 1);
print("\n Using bitwise : (", a ," & (", b ," - 1)) = ", ans2);
}
}
func main()
{
var a: Int = 37;
// b is power of 2
var b: Int = 4;
a = 63;
// b is power of 2
b = 8;
}
main();``````

Output

`````` Given a :  37  b :  4
Using modulus : ( 37  %  4 ) =  1
Using bitwise : ( 37  & ( 4  - 1)) =  1

Given a :  63  b :  8
Using modulus : ( 63  %  8 ) =  7
Using bitwise : ( 63  & ( 8  - 1)) =  7``````
``````// Kotlin Program
// Compute modulus division by a power of 2 number
class Division
{
fun modulusDivision(a: Int, b: Int): Unit
{
print("\n Given a : " + a + " b : " + b);
// Assume b is power of 2
// Case A Using modulus operator
val ans1: Int = a % b;
print("\n Using modulus : (" + a + " % " + b + ") = " + ans1);
// Case A Using bitwise operator
val ans2: Int = a and(b - 1);
println("\n Using bitwise : (" + a + " & (" + b + " - 1)) = " + ans2);
}
}
fun main(args: Array < String > ): Unit
{
var a: Int = 37;
// b is power of 2
var b: Int = 4;
a = 63;
// b is power of 2
b = 8;
}``````

Output

`````` Given a : 37 b : 4
Using modulus : (37 % 4) = 1
Using bitwise : (37 & (4 - 1)) = 1

Given a : 63 b : 8
Using modulus : (63 % 8) = 7
Using bitwise : (63 & (8 - 1)) = 7``````

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.