Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Code problem

0 views
Skip to first unread message

jacob navia

unread,
Dec 6, 2007, 5:06:07 PM12/6/07
to
Consider this:

extern void abort(void);
int main (void)
{
unsigned long long xx;
unsigned long long *x = (unsigned long long *) &xx;

*x = -3;
*x = *x * *x;
if (*x != 9)
abort ();
return(0);
}

lcc-win interprets
*x = -3;
as
*x = 4294967293;
since x points to an UNSIGNED long long.
I cast the 32 bit integer -3 into an unsigned integer
then I cast the result to an unsigned long long.

Apparently gcc disagrees.

Am I doing something wrong somewhere?

I should first cast into a long long THEN into an unsigned
long long?

Thanks for your help.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32

Neil Booth

unread,
Dec 6, 2007, 5:37:41 PM12/6/07
to
jacob navia wrote:

> lcc-win interprets
> *x = -3;
> as
> *x = 4294967293;
> since x points to an UNSIGNED long long.

The std is clear that
*x = -3;
is (with implicit casts)
*x = (unsigned long long) -(int)3;

Assuming your ULL is 64bit, this value is too
big to go in 32 bits.

Neil.

Bart van Ingen Schenau

unread,
Dec 7, 2007, 6:35:28 AM12/7/07
to
On 6 dec, 23:06, jacob navia <ja...@nospam.com> wrote:
> Consider this:
>
> extern void abort(void);
> int main (void)
> {
> unsigned long long xx;
> unsigned long long *x = (unsigned long long *) &xx;
>
> *x = -3;
> *x = *x * *x;
> if (*x != 9)
> abort ();
> return(0);
>
> }
>
> lcc-win interprets
> *x = -3;
> as
> *x = 4294967293;

That is definately not correct.

> since x points to an UNSIGNED long long.
> I cast the 32 bit integer -3 into an unsigned integer
> then I cast the result to an unsigned long long.

The expression -3 has the type 'signed int'. This is not affected by
the type you are assigning to.
As part of the assignment, the type must be converted to 'unsigned
long long'. This is conceptually a single-step conversion.
If your compiler internally can't handle the conversion from 'signed
int' to 'unsigned long long' in one step, you must use a sequence that
yields the exact same result, which would be to widen the type first
and only afterwards convert from signed to unsigned.

>
> Apparently gcc disagrees.
>
> Am I doing something wrong somewhere?
>
> I should first cast into a long long THEN into an unsigned
> long long?

Yes.

>
> Thanks for your help.
>
Bart v Ingen Schenau

0 new messages