简体   繁体   English

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

[英]Convert long to two int and vice versa

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

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中,您不需要太多括号,也不需要反向计算任何掩码。

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的评论编辑)

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

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