我想将int64的低32位作为int32 [英] I want to get the low 32 bit of a int64 as int32

查看:416
本文介绍了我想将int64的低32位作为int32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Int64值,但是我只需要低32位.因此,我想要一种快速的方法来从Int64值的低32位获取Int32值.

I have an Int64 value, but I only need the lower 32 bits. Thus I want a quick way to get the Int32 value from the lower 32 bits of the Int64 value.

谢谢

推荐答案

执行以下操作:

long tempLong = ((yourLong >> 32) << 32); //shift it right then left 32 bits, which zeroes the lower half of the long
int yourInt = (int)(yourLong - tempLong);

这可能不是最紧凑的方法,但对我来说似乎最具可读性. 以下代码将提取long的高半部分:

This may not be the most compact way to do it, but it seems to be the most readable to me. The following code will extract the high half of the long:

long tempLong = (int)(yourLong >> 32);

这篇关于我想将int64的低32位作为int32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆