如何用位操作求余
题目如下:
int a;
a%32
如何用位操作来优化这个求余呢?
谢谢
[解决办法]
[code=Java]public class mod{
public static void main(final String[] args)
{
System.out.println(modBy32(33));
System.out.println(modBy32(6));
System.out.println(modBy32(63));
System.out.println(modBy32(-33));
System.out.println(-33 % 32);
}
static int modBy32(int in){
int ret;
if(in > 0){
if(in < 32) return in;
while((ret = in - (in >> 5)*32) > 32);
return ret;
}
int input = -in;
while((ret = input - (input >> 5)*32) > 32);
return -ret;
}
}
[/java]
[解决办法]
public class Test01 { public static void main(String args[]) { int a = 34; System.out.println(a % 32); System.out.println(a - (a >> 5 << 5)); }}