作者PttHuge (我要看Naruto >_<)
看板java
标题Re: [问题] Java Puzzlers Quiz 3
时间Mon Apr 10 03:20:03 2006
※ 引述《PttHuge (我要看Naruto >_<)》之铭言:
: public class LongDivision {
: public static void main(String[] args) {
: final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
: final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
: System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);
: }
: }
<D>
延伸问题
自己尝试做一个简单的overflow detect
(google一下可以发现JVM 也不会帮你做,所以才要自己做:p)
final int INT_MAX = 0x11111111;
if(INT_MAX & int(MICROS_PER_DAY >> 8) != 0)
System.err.println("Overflow");
if(INT_MAX & int(MILLIS_PER_DAY >> 8) != 0)
System.err.println("Overflow");
结果发现两个都会overflow,有人可以解释一下吗? @_@
不用 final long LONG_MAX 是因为 & 不能用於long
所以改用int 并搭配将MICROS_PER_DAY及MILLIS_PER_DAY right shift 8 bit
--
嘿嘿..我是huge..
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.114.206.35