Jeremy Todd wrote:
> I need to compute the absolute value of the minimum signed int value
> for each signed integral type. This value isn't representable as a
> signed int, but it is representable as an unsigned int.
> This is easy when a larger signed type is available:
> int32_t min_int32 = std::numeric_limits<int32_t>::min();
> int64_t abs_min_int32= -int64_t(min_int32);
> uint32_t answer = uint32_t(abs_min_int32);
> I couldn't come up with an elegant solution when attempting this for
> the 64-bit type. With a two's complement representation, it just so
> happens that reinterpreting the min signed int as an unsigned value
> produces the correct result:
> int64_t min_int64 = std::numeric_limits<int64_t>::min();
> uint64_t answer = uint64_t(min_int64);
> but this felt rather inelegant. Can anyone think of a more elegant
> solution that does not assume a two's complement binary
> representation?
Casting a signed type to an unsigned one has a well-defined modular