概述
看来来晚了...
扔个代码闪人了吧
/**
* 如何生成二进制全1的数
* @author Jeky
*/
public class Rotate {
private static final long TIME = 1000000L;
private static final int LENGTH = 10;
public static void main(String[] args) {
test(new Runnable() {
public void run() {
generateHighBits1(LENGTH);
}
});
test(new Runnable() {
public void run() {
generateHighBits2(LENGTH);
}
});
test(new Runnable() {
public void run() {
generateHighBits3(LENGTH);
}
});
test(new Runnable() {
public void run() {
generateHighBits4(LENGTH);
}
});
}
public static void test(Runnable run) {
long start = System.nanoTime();
for (int i = 0; i < TIME; i++) {
run.run();
}
long end = System.nanoTime();
System.out.println(end - start);
}
public static int generateHighBits1(int length) {
return (int) (Math.pow(2, length) - 1);
}
public static int generateHighBits2(int length) {
return -1 >>> -length;
}
public static int generateHighBits3(int length) {
return HIGH_BITS[length];
}
public static int generateHighBits4(int length) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < length; i++) {
buf.append(1);
}
return Integer.parseInt(buf.toString(), 2);
}
private static final int[] HIGH_BITS = new int[32];
static {
for (int i = 0; i < HIGH_BITS.length; i++) {
HIGH_BITS[i] = generateHighBits1(i);
}
}
}
最后
以上就是落寞狗为你收集整理的Java奇怪的位移_Java中位移的疑惑的全部内容,希望文章能够帮你解决Java奇怪的位移_Java中位移的疑惑所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复