Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Using inline in GCC

Author: Eugene Nalimov

Date: 12:51:52 07/25/04

Go up one level in this thread


On July 25, 2004 at 08:44:32, Omid David Tabibi wrote:

>[...]
>
>#include <stdio.h>
>
>int main()
>{
>    unsigned __int64 x, y;
>
>    x = 1 << 60;
>    y = 1ui64 << 60;
>
>    printf ("x = %I64u, y = %I64u\n", x, y);
>
>    return 0;
>}
>
>The output is:
>x = 0, y = 1152921504606846976

Yes, and here Visual C is doing everything according to the C/C++ Standards.
Constants "1" and "60" have integer type. No promotions to longer types are
necessary to evaluate (integer << integer), so compiler uses integer shift.
C/C++ Standards explicitly say "if shift amount is less than zero, or larger or
equal than bit size of the type, result is undefined" (not exact quote, my
copies of the Standards are in my office, and I am writing this from memory).
Please notice that results are undefined, not unspecified or
implementation-dependent, so your C program is incorrect.

I believe other x86 C compilers will also produce not the result you are
expecting.

Visual C 2005 emits warning for this case (I agree it could be done earlier, but
better late than never). If you are MSDN subscriber you can download Visual
Studio 2005 beta from Microsoft site.

BTW you can observe even more interesting behavior for the slightly modified
version of that incorrect C program. The following program (compiled by Visual
C) will produce different results when compiled with optimizations disabled
(/Od) and enabled (/O2):

#include <stdio.h>

int main ()
{
    unsigned __int64 x;
    int c;

    c = 60;
    x = 1 << c;

    printf ("%I64u\n", x);
    return 0;
}

Thanks,
Eugene



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.