# What is the difference between y=-1 and y=0xFFFFFFFF?

-4
``````y=-1;
y=0xFFFFFFFF;
``````

The question is, is there a difference between these two statements in your code? Explain. If there is a difference, make sure to provide an example.

I was thinking there is no difference, but if the variable y is unsigned, there is a big difference?

c

1
``````y=-1;
y=0xFFFFFFFF;
``````

`-1` is an expression of type `int`, with the value negative one.

`0xFFFFFFFF` is a constant of type `int`, `unsigned int`, `long int`, or `unsigned long int`, depending on the implementation. Its value is `4294967295` (232-1). Note that it never has a negative value.

If `y` is of a signed integer type, the first line assigns the value `-1` to `y`. If `y` is of an unsigned integer type, the value `-1` is converted to `y`'s type, yielding the maximum value of its type.

If `4294967295` is within the range of `y`'s type, then the second line assigns the value `4294967295` to `y`.

If it isn't, then if `y` is of an unsigned type, the value `4294967295` is reduced modulo the maximum value of the type plus one. For example, if `y` is of a 16-bit unsigned type, the value assigned is `65535`. If `y` is of a signed type that's not big enough to hold `4294967295`, then the implicit conversion yields an implementation-defined result. Most commonly, the high-order bits are discarded, but that's not required.

For example, if `y` is of a 32-bit signed integer type, then `y=0xFFFFFFFF` will most likely assign the value `-1` to `y`.

Finally, converting an out-of-range value to a signed integer type can in principle raise an implementation-defined signal (starting with C99), but I don't know of any implementations that do this.

I've glossed over the possibility of padding bits. I've also ignored the possibility that `y` is of a floating-point or complex type.

This answer could have been a lot shorter if you had told us how `y` is declared, and what the range of `y`'s type is on the implementation you're using.

0

four differences:

1. If your integer type is larger than 32 bits, the values are different.
2. One constant is signed and the other is unsigned. Generally, this won't make a difference if you assign to a 32 bit integer, but some code checkers may complain.
3. If your dialect of C is sufficiently old, the first line is equivalent to: y = y - 1; (That is a leftover from C version 6. Very very old.) Even so, write it as: y = -1;
4. Things get really weird if your integer system isn't twos complement.

User contributions licensed under CC BY-SA 3.0