0

I know that in C the conversion of unsigned to signed integers is implementation defined, but what is it for C++? I figured someone would have asked this already, and I searched but I couldn't find it.

I have a function that operates on an unsigned integer and returns a related unsigned integer. I am passing that function a signed integer by casting to unsigned similar to int num = -6; unsigned ret = func((unsigned)num); int ret_as_signed = (int)ret;. In Visual Studio that works fine, but I wonder how portable it is.

Is there a portable way to convert unsigned integers to signed integers? It it possible to just reverse how signed integers are converted to unsigned via wraparound? Thanks

loop
  • 3,460
  • 5
  • 34
  • 57
  • possible duplicate of [How portable is casting -1 to an unsigned type?](http://stackoverflow.com/questions/1667963/how-portable-is-casting-1-to-an-unsigned-type) – jterrace Mar 25 '14 at 00:57
  • 1
    @jterrace Disagree. That's conversion of signed to unsigned, which I know is wraparound. What I want to know about is conversion of unsigned to signed. – loop Mar 25 '14 at 01:01
  • @test Aren't you concerned about the conversion of `-6` to `unsigned int`? – R Sahu Mar 25 '14 at 02:23
  • In C it is "implementation-defined or an implementation-defined signal is raised". C++ doesn't have the provision to raise a signal, as rici points out. – M.M Mar 25 '14 at 02:37
  • This question is now answered but I have found a related question here in case anyone is interested: http://stackoverflow.com/q/19842215/894506 – loop Mar 25 '14 at 03:40

2 Answers2

6

Since C++20 finally got rid of ones' complement and sign-magnitude integers, conversion between signed and unsigned integers is well-defined and reversible. All standard integer types are now 2's complement and conversion between signed and unsigned does not alter any bits in the representation.

For versions of C++ prior to C++20, the original answer still applies. I'm leaving it as a historical remnant.


Conversion of an unsigned integer to a signed integer where the unsigned value is outside of the range of the signed type is implementation-defined. You cannot count on being able to round-trip a negative integer to unsigned and then back to signed. [1]

C++ standard, [conv.integral], § 4.7/3:

If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined.

[1] It seems likely that it will work, but there are no guarantees.

rici
  • 234,347
  • 28
  • 237
  • 341
  • 1
    Note, that since C++ it's no longer implementation-defined and is just defined. – Dan M. May 25 '21 at 12:42
  • @danm: I think you meant "since C++20". That's correct, and there must be hundreds of SO posts which need to be updated. Happy hunting! (I'll see what I can find in my posts a bit later.) – rici May 25 '21 at 16:01
6

For the portable version of the inverse of signed->unsigned conversion, how about:

if ( ret <= INT_MAX )
    ret_as_signed = ret;
else
    ret_as_signed = -(int)(UINT_MAX - ret) - 1;

You could probably generalize this using the templates in <limits>.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • This is also a good answer. I asked a two part question and rici answered one part and you answered the other. Unfortunately there is no way to mark both as correct. – loop Mar 25 '14 at 03:38