简体   繁体   English

在64位长中包装4个整数-Java按位

[英]Wrapping 4 integers in a 64 bit long - java bitwise

Alright, so I have 4 integers I want to wrap in a long. 好了,所以我有4个整数要打包。 The 4 integers all contains 3 values, positioned in the first 2 bytes: 这4个整数全部包含3个值,位于前2个字节中:

 +--------+--------+
 |xxpppppp|hdcsrrrr|
 +--------+--------+

{pppppp} represents one value, {hdcs} represents the second and {rrrr} the last. {pppppp}代表一个值,{hdcs}代表第二个值,{rrrr}代表最后一个值。

I want to pack 4 of these integers, in a long. 我想长时间打包这些整数中的4个。 I've tried the following: 我尝试了以下方法:

ordinal = (c1.ordinal() << (14*3) | c2.ordinal() << (14*2) | c3.ordinal() << 14 | c4.ordinal());

where c1.ordinal()...c4.ordinal() is the integers to wrap. 其中c1.ordinal()... c4.ordinal()是要包装的整数。

This does not seem to work if I run a test. 如果我运行测试,这似乎不起作用。 Lets say I want to look up the values of the last integer in the long, c4.ordinal() , where {pppppp} = 41, {hdcs} = 8 and {rrrr} = 14, I get the following results: 假设我要查找long c4.ordinal()最后一个整数的值,其中{pppppp} = 41,{hdcs} = 8,{rrrr} = 14,我得到以下结果:

System.out.println(c4.ordinal() & 0xf); //Prints 14
System.out.println(hand.ordinal() & 0xf); // Prints 14 - correct

System.out.println(c4.ordinal() >> 4 & 0xf); // Prints 8
System.out.println(hand.ordinal() >> 4 & 0xf); // Prints 8 - correct

System.out.println(c4.ordinal() >> 8 & 0x3f); // Prints 41
System.out.println(hand.ordinal() >> 8 & 0x3f); // Prints 61 - NOT correct!

Now, the following is weird to me. 现在,以下对我来说很奇怪。 If I remove the first two integers, and only wrap the last two, like this: 如果我删除前两个整数,而只包装后两个,则如下所示:

ordinal = (c3.ordinal() << 14 | c4.ordinal());

And run the same test, I get the correct result: 并运行相同的测试,我得到正确的结果:

System.out.println(c4.ordinal() >> 8 & 0x3f); // Prints 41
System.out.println(hand.ordinal() >> 8 & 0x3f); // Prints 41 - correct!

I have no idea whats wrong. 我不知道怎么了 And it does not make any sense to me, that I get the correct answer if I remove the first two integers. 对于我来说,如果删除前两个整数,我会得到正确的答案,这对我没有任何意义。 I'm starting to thing this might have to do with the long datatype, but I've not found anything yet, that supports this theory. 我开始认为这可能与长数据类型有关,但是我还没有发现任何支持这种理论的东西。

Even though you are assigning the result to a long , all of the operations are performed with int values, and so the high-order bits are lost. 即使将结果分配给long ,所有操作都使用int值执行,因此高阶位会丢失。 Force "promotion" to a long by explicitly widening the values to a long . 力“促销”的long通过明确扩大值一long

long ordinal = (long) c1.ordinal() << (14*3) | 
               (long) c2.ordinal() << (14*2) | 
               (long) c3.ordinal() <<    14  | 
               (long) c4.ordinal();

Also, unless you are positive that the top two bits of each value are zero, you could run into other problems. 另外,除非您肯定每个值的高两位为零,否则可能会遇到其他问题。 You may wish to mask these off for safety's sake: 为了安全起见,您可能希望屏蔽这些:

long ordinal = (c1.ordinal() & 0x3FFFL) << (14*3) | 
               (c2.ordinal() & 0x3FFFL) << (14*2) | 
               (c3.ordinal() & 0x3FFFL) <<    14  | 
               (c4.ordinal() & 0x3FFFL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM