feat(REQ-5865) - 调整 MQ 事件实现算 boolean 的逻辑
This commit is contained in:
parent
a9a90ab46d
commit
167298881b
@ -81,19 +81,30 @@ public class ImplementationReadyChecker implements ApplicationListener<Applicati
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 自适应转换:value ≤15 时返回 4 位数组,否则返回最小所需位数数组
|
||||
*/
|
||||
public static boolean[] toBooleansAdaptive(int value) {
|
||||
if (value < 0) throw new IllegalArgumentException("adaptive mode only for non-negative");
|
||||
if (value == 0) return new boolean[]{false};
|
||||
int bits = 32 - Integer.numberOfLeadingZeros(value);
|
||||
if (value < 0) {
|
||||
throw new IllegalArgumentException("adaptive mode only for non-negative");
|
||||
}
|
||||
|
||||
// 核心修改:value ≤15 强制用 4 位,否则计算最小所需位数
|
||||
int bits = (value <= 15) ? 4 : (32 - Integer.numberOfLeadingZeros(value));
|
||||
return toBooleans(value, bits);
|
||||
}
|
||||
|
||||
// 高位在前:index=0 是最高位
|
||||
/**
|
||||
* 将 value 转换为指定位数的 boolean 数组(高位在前)
|
||||
*/
|
||||
public static boolean[] toBooleans(int value, int bits) {
|
||||
if (bits <= 0 || bits > 32) throw new IllegalArgumentException("bits must be 1~32");
|
||||
if (bits <= 0 || bits > 32) {
|
||||
throw new IllegalArgumentException("bits must be 1~32");
|
||||
}
|
||||
|
||||
boolean[] arr = new boolean[bits];
|
||||
for (int i = 0; i < bits; i++) {
|
||||
int shift = bits - 1 - i;
|
||||
int shift = bits - 1 - i; // 从高位到低位遍历
|
||||
arr[i] = ((value >>> shift) & 1) == 1;
|
||||
}
|
||||
return arr;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user