6

I've done some quick tests that a signed int to unsigned int cast in C does not change the bit values (on an online debugger).

What I want to know is whether it is guaranteed by a C standard or just the common (but not 100% sure) behaviour ?

11
  • 2
    It changes the context, that is, what the value means. All computer data consists of a limited set of numbers, which have meaning in context. However it is rather different when casting, say, int to float, when the representation will change. Oct 16, 2019 at 14:33
  • Only an assignment would change a value. Casting from signed (2s-complement) to unsigned is an unsafe cast, and I think, implementation specific. Oct 16, 2019 at 14:35
  • Possible duplicate? stackoverflow.com/questions/50605/… Oct 16, 2019 at 14:38
  • 1
    @LegendofPedro it would be safe with two int values which are known to be non-negative, and you want their sum without danger of int overflow. Oct 16, 2019 at 14:40
  • 2
    @WeatherVane (and LegendofPedro): The cast is perfectly well-defined, without danger of overflow, regardless of the sign of the signed int going in. Oct 16, 2019 at 14:50

2 Answers 2

7

Conversion from signed int to unsigned int does not change the bit representation in two’s-complement C implementations, which are the most common, but will change the bit representation for negative numbers, including possible negative zeroes on one’s complement or sign-and-magnitude systems.

This is because the cast (unsigned int) a is not defined to retain the bits but the result is the positive remainder of dividing a by UINT_MAX + 1 (or as the C standard (C11 6.3.1.3p2) says,

the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

The two’s complement representation for negative numbers is the most commonly used representation for signed numbers exactly because it has this property of negative value n mapping to the same bit pattern as the mathematical value n + UINT_MAX + 1 – it makes it possible to use the same machine instruction for signed and unsigned addition, and the negative numbers will work because of wraparound.

2

Casting from a signed to an unsigned integer is required to generate the correct arithmetic result (the same number), modulo the size of the unsigned integer, so to speak. That is, after

int i = anything;
unsigned int u = (unsigned int)i;

and on a machine with 32-bit ints, the requirement is that u is equal to i, modulo 232.

(We could also try to say that u receives the value i % 0x100000000, except it turns out that's not quite right, because the C rules say that when you divide a negative integer by a positive integer, you get a quotient rounded towards 0 and a negative remainder, which isn't the kind of modulus we want here.)

If i is 0 or positive, it's not hard to see that u will have the same bit pattern. If i is negative, and if you're on a 2's complement machine, it turns out the result is also guaranteed to have the same bit pattern. (I'd love to present a nice proof of that result here, but I don't have time just now to try to construct it.)

The vast majority of today's machines use 2's complement. But if you were on a 1's complement or sign/magnitude machine, I'm pretty sure the bit patterns would not always be the same.

So, bottom line, the sameness of the bit patterns is not guaranteed by the C Standard, but arises due to a combination of the C Standard's requirements, and the particulars of 2's complement arithmetic.

13
  • 1
    You should express the rules in terms of a two’s complement C implementation, not a two’s complement machine. While C is generally intended to use features matching its target processor, the implementation has the ultimate say. If I write a C implementation with one’s complement to support some ancient software I want to display in a computer museum, it is going to obey the one’s complement rules even if the underlying machine is two’s complement. Oct 17, 2019 at 0:08
  • @EricPostpischil are you aware of existing one's complement machines ? Oct 17, 2019 at 7:35
  • @GuillaumePetitjean: As I just stated, C may be implemented via software rather than hardware. Thus, the underlying machine is not the determining factor. So the existence of machines of any particular type is irrelevant. The rules stated in the C standard are clear: The behavior is defined by the C implementation, not by the hardware. Oct 17, 2019 at 10:25
  • I fully understoodd that @EricPostpischil. But I guess in practice it doesn't make sense to choose a 1's complement C implementation on a 2's complement machine. Just curious. Oct 17, 2019 at 11:36
  • @GuillaumePetitjean: Stating the behavior of C integers is determined by the machine is false. Stating the behavior of C integers is determined by the C implementation is true. It is that simple. One is true, the other is false, teaching students false statements is bad, and relying on false statements sometimes leads to errors in unexpected ways. C compilers have become increasingly aggressive about optimization, taking advantage of rules in the C standard even if they are not a direct outcome of the target hardware. There is no reason to make an incorrect statement here. Oct 17, 2019 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.