java 中将布尔数组转换为整数的方法的最快实现
Fastest implementation of a method that transforms boolean arrays to integers in java
有什么方法可以提高这种方法的速度吗?:
static int booleanArrayToInt(boolean[] array) {
int x = 0;
int i = 0;
for (boolean b : array) {
i++;
if (b) {
x = 1;
break;
}
}
for (int j = i; j < array.length; j++) {
if (array[j]) x = (x << 1) + 1;
else x = x << 1;
}
return x;
}
给你。
boolean[] bools =
{ true, true, false, false, true, true, false, false, true
};
public static int binaryToInt(boolean[] bools) {
int x = 0;
for (boolean b : bools) {
x <<= 1;
x |= b ? 1
: 0;
}
return x;
}
有什么方法可以提高这种方法的速度吗?:
static int booleanArrayToInt(boolean[] array) {
int x = 0;
int i = 0;
for (boolean b : array) {
i++;
if (b) {
x = 1;
break;
}
}
for (int j = i; j < array.length; j++) {
if (array[j]) x = (x << 1) + 1;
else x = x << 1;
}
return x;
}
给你。
boolean[] bools =
{ true, true, false, false, true, true, false, false, true
};
public static int binaryToInt(boolean[] bools) {
int x = 0;
for (boolean b : bools) {
x <<= 1;
x |= b ? 1
: 0;
}
return x;
}