Precision loss when converting from int64 and uint64 C++?

Viewed 213

Let's say I have the following code fragment in C++:

int64_t a = VALUE;

uint64_t b = a;

int64_t c = b;

Is there a VALUE for which a != c?

1 Answers

Is there a VALUE for which a != c?

Pre C++20

Yes, theoretically any value less than 0 will cause you an issue. With a value less than 0, b will get a value greater than std::numeric_limits<int64>::max() and that means that there will be an implementation defined conversion from b to c.

For standard desktop machines where all integers are two's complement, then a will equal c. For a machine where signed and unsigned integers have different complement's, a may/will not equal c.

C++20

No, all integer types are two's complement so the value will be preserved.

Related