Getting 32 bit words out of 64-bit values in C/C++ and not worrying about endianness

bit-manipulation, bitwise-operators, c, c++, endianness

Solution

6.5.7 Bitwise shift operators

4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

So, yes -- guranteed by the standard.

Problem

It's my understanding that in C/C++ bitwise operators are supposed to be endian independent and behave the way you expect. I want to make sure that I'm truly getting the most significant and least significant words out of a 64-bit value and not worry about endianness of the machine. Here's an example: ``` uint64_t temp; uint32_t msw, lsw; msw = (temp & 0xFFFFFFFF00000000) >> 32; lsw = temp & 0x00000000FFFFFFFF; ``` Will this work?

Original source

Related problems