Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a uint64 whose lower and upper bits reference uint32s?

Tags:

c++

auto lo = 0u;
auto hi = 1u;
auto combined = (((std::uint64_t)hi) << 32) | lo;

But if I do combined++ then lo and hi are not changed, because combined is a copy.

How do I make the lower bits of combined reference lo, and the upper bits reference hi, so when I do arithmetic on combined, lo and hi are also updated?

like image 764
Wirable2323 Avatar asked Jan 22 '26 04:01

Wirable2323


1 Answers

I would sidestep the whole issue by creating simple get_lo () and get_hi () accessor functions to extract the relevant halves of your int64 and call them when needed:

#include <cstdint>

std::uint32_t get_lo (std::uint64_t combined) { return static_cast <std::uint32_t> (combined); }

std::uint32_t get_hi (std::uint64_t combined) { return static_cast <std::uint32_t> (combined >> 32); }

I really don't see the need for anything more elaborate.

like image 180
Paul Sanders Avatar answered Jan 23 '26 20:01

Paul Sanders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!