private void test2() {
// This test takes two shorts and sticks them together in a
// 4 bit 12 bit configuration within a short, it then breaks
// them apart again to see if it worked!
short s0 = 4095;
short s1 = 13;
short sh = (short)((s1 << 12) | s0);
System.out.println(sh);
short[] sa = new short[] {
(short)(sh & 0xFFF),
(short)((sh >>> 12) & 0xF)
};
System.out.println(sa[0]);
System.out.println(sa[1]);
}
我对此的期望是这样的;
s0
二进制是b0000_1111_1111_1111
s1
二进制是b0000_0000_0000_1101
sh
然后变成b1101_1111_1111_1111
前面1
是符号,其余 15 位给出sh
十进制值,-24575
但这不是我输出到控制台的值(即-8193
)。
我怎么了?