For example:
- INT64_MIN to 0
- 0 to INT64_MAX+1
- INT64_MAX to UINT64_MAX
Below is my code:
uint64_t int64ToUint64(int64_t value)
{
uint64_t uvalue = value - std::numeric_limits<int64_t>::min();
return uvalue;
}
It works, but there is signed integer overflow, and per C++ standard, signed integer overflow is behavior undefined. Any other solutions without integer overflow?
Since conversion from signed to unsigned types is well defined and since there's no overflow on unsigned arithmetic, you can do what you want safely by first converting the signed value to an unsigned type: