String strs = "13634393448";
Bytes.toBytes((short) (strs.hashCode() & 0x7fff));
这里面的 & 0x7fff 是什么意思呢?
巴扎黑2017-04-18 09:45:40
0x07fff=0111111111111111
What will happen if you do and operate this thing? The first bit is 0, and the other bits remain unchanged.
Depending on the data type, the higher bit in the signed data type represents the symbol. In this case, the AND operation is used to obtain the absolute value, that is, to obtain the useful 15 bits.
Sometimes bitwise search or bitwise operation is relatively faster.
There is another situation where it is used to show off.
怪我咯2017-04-18 09:45:40
String.hashCode()
的返回值计算方式为:s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
你给的13634393448,其hashCode结果是1002012672
,也就是0x3bb98000
。
同0x7fff
做"与"操作,也就是表示取0x3bb98000
后三位的值,本题的结果自然就是0
.
PHPz2017-04-18 09:45:40
0x7fff = 0111 1111 1111 1111
& is an AND operation
So this operation is to take the last 15 digits of the binary representation of the number "13634393448"