I want to get the low 32 bit of a int64 as int32

前端 未结 5 1267
梦如初夏
梦如初夏 2021-01-04 05:02

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.

Thanks

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-04 05:19

    Do something like this:

    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);
    

    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);
    

提交回复
热议问题