我与换挡玩,我让陷入困境的一个案例:转移超过类型位大小
int maxint = Integer.MAX_VALUE;
LOG.debug("maxint << 31 ---> {} ({})", maxint << 31 , Integer.toBinaryString(maxint << 31));
LOG.debug("maxint << 32 ---> {} ({})", maxint << 32 , Integer.toBinaryString(maxint << 32));
LOG.debug("maxint << 33 ---> {} ({})", maxint << 33 , Integer.toBinaryString(maxint << 33));
和它打印:
maxint << 31 ---> -2147483648 (10000000000000000000000000000000)
maxint << 32 ---> 2147483647 (1111111111111111111111111111111)
maxint << 33 ---> -2 (11111111111111111111111111111110)
所以问题是,如果移位31个叶“1”在MSB处,班次32不应移出并返回0?
进一步说我做了同一起跑线轮班31的结果(这是Integer.MIN_VALUE的)和1
int minInt = -2147483648;
LOG.debug("minInt << 1 ---> {} ({})", minInt << 1 , Integer.toBinaryString(minInt << 1));
LOG.debug("minInt << 2 ---> {} ({})", minInt << 2 , Integer.toBinaryString(minInt << 2));
转移和它打印:
minInt << 1 ---> 0 (0)
minInt << 2 ---> 0 (0)
这是我所期望。
小心一点。这只适用于正数。 '-1%32 = -1' – harold 2014-08-28 12:53:52