Java Puzzlers笔记--puzzle 5: The joy of Hex 十六进制计算问题
2007-03-03 19:09
741 查看
public class JosOfHex{
public void main(String[] args){
System.out.println(
Long.toHexString(0x100000000 + 0xcafebabe));
}
}
Solution:
显示:cafebabe
由于0x100000000在int中的溢出表示为0x 0000 0001 0000 0000L
而 0xcafebabe 在int中的溢出表示为0x ffff ffff cafe babeL 因为高位为1是,对int扩展为
long是高32位表示为ffff ffff;
TID:
negative decimal constants are clearly identifiable by the presentce of a minus
sign.
Hex and octal literals are negative if theic high-order bit is set.
it is generally best to avoid mixed-type computations.
Correctly:
public class JoyOfHex{
public static void main(String[] args){
System.out.println(Long.toHexString(0x100000000L + 0xcafebabeL));
}
}
public void main(String[] args){
System.out.println(
Long.toHexString(0x100000000 + 0xcafebabe));
}
}
Solution:
显示:cafebabe
由于0x100000000在int中的溢出表示为0x 0000 0001 0000 0000L
而 0xcafebabe 在int中的溢出表示为0x ffff ffff cafe babeL 因为高位为1是,对int扩展为
long是高32位表示为ffff ffff;
TID:
negative decimal constants are clearly identifiable by the presentce of a minus
sign.
Hex and octal literals are negative if theic high-order bit is set.
it is generally best to avoid mixed-type computations.
Correctly:
public class JoyOfHex{
public static void main(String[] args){
System.out.println(Long.toHexString(0x100000000L + 0xcafebabeL));
}
}
相关文章推荐
- Java Puzzlers笔记--puzzle 7: Swap Meat ^符号问题
- Java Puzzlers笔记--puzzle 8: Dos Equis 选择操作符问题
- Java Puzzlers笔记--puzzle 15: Hello Whirled -Unicode字符问题
- Java Puzzlers笔记--puzzle 21: What's my Class, take 2 路径字符问题
- Java Puzzlers笔记--Puzzle 3: Long Division 溢出问题
- Java Puzzlers笔记--puzzle 22:Dupe of URL url的使用
- Java Puzzlers笔记--puzzle 9: Tweedledum +=的问题
- Java Puzzlers笔记--puzzle 11: The last laugh "" 与''的区别
- Java Puzzlers笔记--puzzle 10: Tweedledee += 问题(2)
- Java Puzzlers笔记--puzzle 19: Classy Fire 注释问题
- Java Puzzlers笔记--Puzzle 2: Time for a change 关于浮点型的问题
- Java Puzzlers笔记--puzzle 13: Animal Farm 优先级以及对象引用问题
- Java Puzzlers笔记--puzzle 14: Escape Rout (")双引号的问题
- Java Puzzlers笔记--puzzle 17: Huh? 转义字符问题
- 解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题
- maven -- 问题解决(三)Java compiler level does not match the version of the installed Java project facet
- 【转】解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题
- 读Java Puzzlers笔记: 表达式问题
- 完美解决java compiler level does not match the version of the installed java project facet问题
- Java compiler level does not match the version of the installed Java project facet等常见编译器问题