Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f7df381

Browse files
author
Ram swaroop
committed
n modulo d without division(/) and modulo(%) operators, where d is a power of 2 number
1 parent f78ff8d commit f7df381

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/me/ramswaroop/bits/Modulo.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package me.ramswaroop.bits;
2+
3+
/**
4+
* Created by IntelliJ IDEA.
5+
*
6+
* @author: ramswaroop
7+
* @date: 6/8/15
8+
* @time: 11:28 PM
9+
*/
10+
public class Modulo {
11+
12+
public static int getNmoduloD(int n, int d) {
13+
return n & (d - 1);
14+
}
15+
16+
public static void main(String a[]) {
17+
System.out.println(getNmoduloD(18, 8));
18+
System.out.println(getNmoduloD(18, 4));
19+
System.out.println(getNmoduloD(13, 4));
20+
System.out.println(getNmoduloD(13, 1));
21+
System.out.println(getNmoduloD(2, 2));
22+
System.out.println(getNmoduloD(13, 16));
23+
}
24+
}
25+
26+
/**
27+
* For example,
28+
*
29+
* 18 = 10010
30+
* 8 = 01000
31+
* 2 = 00010 (remainder = 18 & (8-1))
32+
*/

0 commit comments

Comments
 (0)