java 将 long 转换为两个 int,反之亦然

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10686178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:06:15  来源:igfitidea点击:

Convert long to two int and vice versa

javaintlong-integer

提问by user1293081

How can I convert two 32 bit integers (int) to one 64 bit longand vice versa?

如何将两个 32 位整数 ( int)转换为一个 64 位整数long,反之亦然?

回答by Fuwjax

long c = (long)a << 32 | b & 0xFFFFFFFFL;
int aBack = (int)(c >> 32);
int bBack = (int)c;

In Java, you don't need quite so many parentheses, or any masking on the reverse calculation.

在 Java 中,您不需要太多括号,也不需要对反向计算进行任何掩码。

回答by Oliver Charlesworth

Ints to longs:

整数到多头:

long c = ((long)a << 32) | ((long)b & 0xFFFFFFFFL);

I'll leave it as an exercise for the reader to perform the reverse calculation. But the hint is; use more bit-shifts and bit-masks.

我将把它作为练习供读者执行反向计算。但提示是;使用更多的位移位掩码

(Edited as per comment by T. Murdock)

(根据 T. Murdock 的评论编辑)