Re: Signed and unsigned types issue
firstname.lastname@example.org (pozz) writes:
> I know it is dangerous to mix signed and unsigned types, and this
> situation is an example.
> With a compiler for a 16-bit embedded CPU, the following piece of code
> results in an error:
> unsigned int foo(void);
> long value;
> value = -foo();
> If foo() returns 60, value will be 65476 and not -60. I solved adding a
> cast (considering that foo() returns small values, between 10 and 60, this
> should always work):
> value = -(int)foo();
If you want a long, why are you casting to int?
value = -(long)foo();
> Anyway I want to understand what happens without the cast. Is the minus
> sign (sign inversion) applied to an unsigned type before assigning the
> result to the long (signed) type?
Well, clearly the unary negation is applied to an unsigned type, that's
correct. However, the unsigned int that you are left with after that
negation is then converted to type long (retaining its value by zero
extension) before the assignment takes place.
Regarding TSA regulations:
How are four small bottles of liquid different from one large bottle?
Because four bottles can hold the components of a binary liquid explosive,
whereas one big bottle can't. -- camperdave responding to MacAndrew on /.
|All times are GMT. The time now is 07:23 PM.|
Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.