"luke" <(E-Mail Removed)> writes:

> I have the following piece of code:

>

> long long ll;

> unsigned int i = 2;

> ll = -1 * i;

> printf("%lld\n", ll);

>

> Why this prints 4294967294 instead of -2?
In

ll = -1 * i;

the expression -1 is of type int, and i is of type unsigned int, so

the -1 is converted to unsigned int before the multiplication.

Converting -1 to unsigned int yields UINT_MAX, which happens to be

4294967295 in your implementation. Multiplying 4294967295 by 2 wraps

around (because it's unsigned arithmetic), yielding 4294967294.

--

Keith Thompson (The_Other_Keith)

http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>

San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>

We must do something. This is something. Therefore, we must do this.