Java java.lang.Integer class中的代码注释'HD, Figure'是什么意思?
What does the code comment 'HD, Figure' mean in Java java.lang.Integer class?
例如,JDK 方法 java.lang.Integer.numberOfLeadingZeros(int):
public static int numberOfLeadingZeros(int i) {
// HD, Figure 5-6
if (i == 0)
return 32;
int n = 1;
if (i >>> 16 == 0) { n += 16; i <<= 16; }
if (i >>> 24 == 0) { n += 8; i <<= 8; }
if (i >>> 28 == 0) { n += 4; i <<= 4; }
if (i >>> 30 == 0) { n += 2; i <<= 2; }
n -= i >>> 31;
return n;
}
代码注释'HD,图5-6'是什么意思?
HD = 黑客的喜悦。见 the javadoc:
Implementation note: The implementations of the "bit twiddling" methods (such as highestOneBit
and numberOfTrailingZeros
) are based on material from Henry S. Warren, Jr.'s Hacker's Delight, (Addison Wesley, 2002).
java.lang.Long
和java.lang.Math
也有这样的评论。
例如java.lang.Math
中的addExact
方法:
public static int addExact(int x, int y) {
int r = x + y;
// HD 2-12 Overflow iff both arguments have the opposite sign of the result
if (((x ^ r) & (y ^ r)) < 0) {
throw new ArithmeticException("integer overflow");
}
return r;
}
Hacker's Delight的相关信息也可以参考:http://hackersdelight.org/
例如,JDK 方法 java.lang.Integer.numberOfLeadingZeros(int):
public static int numberOfLeadingZeros(int i) {
// HD, Figure 5-6
if (i == 0)
return 32;
int n = 1;
if (i >>> 16 == 0) { n += 16; i <<= 16; }
if (i >>> 24 == 0) { n += 8; i <<= 8; }
if (i >>> 28 == 0) { n += 4; i <<= 4; }
if (i >>> 30 == 0) { n += 2; i <<= 2; }
n -= i >>> 31;
return n;
}
代码注释'HD,图5-6'是什么意思?
HD = 黑客的喜悦。见 the javadoc:
Implementation note: The implementations of the "bit twiddling" methods (such as
highestOneBit
andnumberOfTrailingZeros
) are based on material from Henry S. Warren, Jr.'s Hacker's Delight, (Addison Wesley, 2002).
java.lang.Long
和java.lang.Math
也有这样的评论。
例如java.lang.Math
中的addExact
方法:
public static int addExact(int x, int y) {
int r = x + y;
// HD 2-12 Overflow iff both arguments have the opposite sign of the result
if (((x ^ r) & (y ^ r)) < 0) {
throw new ArithmeticException("integer overflow");
}
return r;
}
Hacker's Delight的相关信息也可以参考:http://hackersdelight.org/