From: John Keenan on
Given the following example:

int i20 = 20;
unsigned int ui21 = 21;
int i = ( i20 - ui21 ); // i is set to -1
double d = ( i20 - ui21 ); // d is set to 4294967295.0

Are the results shown correct? What can I search for to find the standard
behavior of calculations involving unsigned int? Since i20 is an int I was
expecting the expression ( i20 - ui21 ) to be an int... but that expectation
was based on gut-feel as opposed to knowledge of the standard.

John


From: David Connet on
"John Keenan" <john.removeme.keenan(a)optimapowerware.com> wrote in
news:Q%AYj.4619$7k7.866(a)flpi150.ffdc.sbc.com:

> Given the following example:
>
> int i20 = 20;
> unsigned int ui21 = 21;
> int i = ( i20 - ui21 ); // i is set to -1
> double d = ( i20 - ui21 ); // d is set to 4294967295.0
>
> Are the results shown correct? What can I search for to find the
> standard behavior of calculations involving unsigned int? Since i20 is
> an int I was expecting the expression ( i20 - ui21 ) to be an int...
> but that expectation was based on gut-feel as opposed to knowledge of
> the standard.

That "is" -1. 4294967295 == 0xFFFFFFFF

When you see funny numbers like that, windows 'calc' (in scientific mode)
is great for switching between hex/dec/oct/bin.

Dave Connet
From: Victor Bazarov on
John Keenan wrote:
> Given the following example:
>
> int i20 = 20;
> unsigned int ui21 = 21;
> int i = ( i20 - ui21 ); // i is set to -1
> double d = ( i20 - ui21 ); // d is set to 4294967295.0
>
> Are the results shown correct? What can I search for to find the standard
> behavior of calculations involving unsigned int? Since i20 is an int I was
> expecting the expression ( i20 - ui21 ) to be an int... but that expectation
> was based on gut-feel as opposed to knowledge of the standard.

Since ui21 is 'unsigned', the Standard says that 'i20' shall be
converted to 'unsigned', so the subexpression 'i20 - ui21' shall have
the type 'unsigned'. The value of it is (2^32 - 1) because arithmetic
operations on unsigned are performed modulo 2^b where 'b' is the number
of bits in the representation. The next subexpression is the assignment
from 'unsigned' to 'double'. Luckily you don't lose any bits here.

See Standard, [expr]/9, to see what's converted to what when expressions
are evaluated.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
From: Vincent Fatica on
On Tue, 20 May 2008 14:19:19 GMT, David Connet <stuff(a)agilityrecordbook.com>
wrote:

>"John Keenan" <john.removeme.keenan(a)optimapowerware.com> wrote in
>news:Q%AYj.4619$7k7.866(a)flpi150.ffdc.sbc.com:
>
>> Given the following example:
>>
>> int i20 = 20;
>> unsigned int ui21 = 21;
>> int i = ( i20 - ui21 ); // i is set to -1
>> double d = ( i20 - ui21 ); // d is set to 4294967295.0
>>
>> Are the results shown correct? What can I search for to find the
>> standard behavior of calculations involving unsigned int? Since i20 is
>> an int I was expecting the expression ( i20 - ui21 ) to be an int...
>> but that expectation was based on gut-feel as opposed to knowledge of
>> the standard.
>
>That "is" -1. 4294967295 == 0xFFFFFFFF

The double 4294967295.0 is not -1.
--
- Vince
From: Pavel A. on
"John Keenan" <john.removeme.keenan(a)optimapowerware.com> wrote in message
news:Q%AYj.4619$7k7.866(a)flpi150.ffdc.sbc.com...
> Given the following example:
>
> int i20 = 20;
> unsigned int ui21 = 21;
> int i = ( i20 - ui21 ); // i is set to -1
> double d = ( i20 - ui21 ); // d is set to 4294967295.0
>
> Are the results shown correct? What can I search for to find the standard
> behavior of calculations involving unsigned int? Since i20 is an int I was
> expecting the expression ( i20 - ui21 ) to be an int... but that
> expectation
> was based on gut-feel as opposed to knowledge of the standard.
>
> John

By C rules, if signed and unsigned types are mixed in one expression,
the signed operands are cast to the unsigned type.
So in your example you get unsigned (positive) ~0 rather than signed -1.

Was this an interview question? :)
--PA