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

5

19

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

Springtime answered 19/9, 2009 at 5:4 Comment(2)
which language is this? please add the correct tagEmersonemery
I don't understand how anyone can answer this question without knowing the language.Thymus
O
11

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);
Octroi answered 19/9, 2009 at 5:11 Comment(2)
Depending on how good the compiler is at optimizing, your first code sample may be substantially slower than just casting. Personally, I don't find it particularly clear, either.Lackadaisical
to zero out the low 32 bits, use yourLong & 0xFFFFFFFF00000000 or yourLong & (~0ULL << 32), but to get the low bits, only a simple assignment is needed because the top will always be truncated, it the compiler warns the use a cast yourInt = (int)yourLongEmersonemery
C
34

If you assign a int64 value into a int32 value, the compiler will automatically do that for you
(as Steven Sudit mentioned):

int64 val64 = ...;
int32 val32 = ...;
...

val32 = val64; // get the low 32 bits
// or
val32 = (val64 >> 32); // get the high 32 bits

and because the compiler may display warnings you can specify the cast

val32 = (int32)val64;
Cabinetmaker answered 19/9, 2009 at 6:8 Comment(1)
Kindly test this with Int64.MaxValue the low 32 bit does not workZama
O
11

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);
Octroi answered 19/9, 2009 at 5:11 Comment(2)
Depending on how good the compiler is at optimizing, your first code sample may be substantially slower than just casting. Personally, I don't find it particularly clear, either.Lackadaisical
to zero out the low 32 bits, use yourLong & 0xFFFFFFFF00000000 or yourLong & (~0ULL << 32), but to get the low bits, only a simple assignment is needed because the top will always be truncated, it the compiler warns the use a cast yourInt = (int)yourLongEmersonemery
L
9

You didn't specify the language, but in many, all you need to do is cast it to an Int32. The top bits will be discarded.

Lackadaisical answered 19/9, 2009 at 5:9 Comment(0)
S
1

You could let the compiler handle the endian-ness, and hide all the bit shifting, pointer manipulation etc.

DWORD CUtility::ConvertUint64toUint32(unsigned __int64 in64){
    ULARGE_INTEGER uli;
    uli.QuadPart = in64;
    return uli.LowPart;
}
Stadia answered 10/9, 2014 at 18:3 Comment(3)
Add 4 spaces in front of your code so that it is properly formatted.Socher
Or, select the block of your code and press Ctrl-K.Wanettawanfried
Did you mean "ConvertUint64toUint32"?Viaduct
H
-3

In C/C++ the best way (in my opinion) is to use a union. For example the following function uses an anonymous union to extract the lower-order 32 bits from a 64 bit integer.

uint32_t lower_32_bits(uint64_t value) {
    union { uint64_t value; struct { uint32_t high, low; }; } converter;
    converter.value = value;
    return converter.low;
}

This union trick can be used for all sorts of things, like getting the bits of a floating point value into an integer of the same bitlength, e.g. for doing bitwise operations and other hacks.

Hurff answered 2/9, 2017 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.