【发布时间】:2022-01-02 05:34:23
【问题描述】:
对于给定的无符号int 值,我尝试了以下方法来存储给定值的位数。
// Returns the number of required bits for (storing) specified unsigned value.
static int size(final int value) {
if (value < 0) {
throw new IllegalArgumentException("value(" + value + ") is negative");
}
return (int) Math.ceil(Math.log10(value) / Math.log10(2));
}
我得到了什么。
size for 1310394095: 31 (RANDOM)
size for 1: 0 (1; Not Good; should be 1)
size_NotNegative_Zero() is @Disabled (0; ERROR; Expecting actual: -2147483648 ...)
size for 2147483647: 31 (Integer.MAX_VALUE)
Integer.MAX_VALUE 的 31 似乎没问题。
我该如何解决这个问题?
【问题讨论】: