Java的存储两个整数在很长 [英] Java storing two ints in a long
本文介绍了Java的存储两个整数在很长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要两个整数存储在一个长的(而不必创建一个新的点
对象的时候)。
目前,我尝试这样做。它不工作,但我不知道什么是错的:
// x和y是整数
长L = X;
L =(L&所述;&下; 32)| ÿ;
和我得到的int类型,像这样:
X =(int)的L>> 32;
Y =(int)的L&安培;为0xffffffff;
解决方案
是
越来越符号扩展的第一个片段,它会覆盖 X
与 1
每当 Y'LT; 0
。
在第二个片段中,投给 INT
是移位之前进行,因此 X
实得的价值的y
。
长L =(((长)x)LT;< 32)| (Y&安培; 0xffffffffL);
INT X =(int)的(L>> 32);
INT Y =(INT)升;
I want to store two ints in a long (instead of having to create a new Point
object every time).
Currently, I tried this. It's not working, but I don't know what is wrong with it:
// x and y are ints
long l = x;
l = (l << 32) | y;
And I'm getting the int values like so:
x = (int) l >> 32;
y = (int) l & 0xffffffff;
解决方案
y
is getting sign-extended in the first snippet, which would overwrite x
with -1
whenever y < 0
.
In the second snippet, the cast to int
is done before the shift, so x
actually gets the value of y
.
long l = (((long)x) << 32) | (y & 0xffffffffL);
int x = (int)(l >> 32);
int y = (int)l;
这篇关于Java的存储两个整数在很长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文