public void set(int bitIndex) {
if (bitIndex < 0)
throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex); int wordIndex = wordIndex(bitIndex);
expandTo(wordIndex); words[wordIndex] |= (1L << bitIndex); // Restores invariants checkInvariants();
}
这个是源码。我想问words是一个long数组,当我set(79)的时候,wordIndex为1,
就是说words[1]|=(1L<<79);
问题来了:1L<<79 不是超出了long的范围了吗?为什么还可以这么做的?
if (bitIndex < 0)
throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex); int wordIndex = wordIndex(bitIndex);
expandTo(wordIndex); words[wordIndex] |= (1L << bitIndex); // Restores invariants checkInvariants();
}
这个是源码。我想问words是一个long数组,当我set(79)的时候,wordIndex为1,
就是说words[1]|=(1L<<79);
问题来了:1L<<79 不是超出了long的范围了吗?为什么还可以这么做的?
突然想了想,发现
1L<<79 == 1L<<15(79%64=15)
然后明白之前文章http://www.cnblogs.com/lqminn/archive/2012/08/30/2664122.html里面所说的“要注意:需要注意的是java中的移位操作会模除位数,也就是说,long类型的移位会模除64”
这么样子后面的就好解释了~