我想将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);