Casting uint64 to uint32 and truncation

192 views Asked by At

When we cast an uint64_t to an uint32_t, I see by reading cppreference that truncation happens.

But, what bits are truncated? The higher or lower bits? How is this decided, and why?

1

There are 1 answers

7
user17732522 On

The resulting value is the unique value equal to the original one modulo 232 that can be represented by uint32_t. In other words, only the 32 least significant bits in the base-2 representation of the value remain and all others (in your case the 32 most significant bits) are truncated.

Whether these bits are high or low (or neither) in the object representation of uint64_t is implementation-defined and depends especially on the endianess of the architecture.