看来来晚了...
扔个代码闪人了吧
/**
* 如何生成二进制全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奇怪内容请搜索靠谱客的其他文章。
发表评论 取消回复