Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Microsoft follies (visual C/C++ 6.0) Answer found. (cough, cough) :)

Author: Uri Blass

Date: 07:29:19 02/08/04

Go up one level in this thread


On February 08, 2004 at 09:59:37, Sune Fischer wrote:

>>
>>Under MSVC, test the following:
>>
>>    unsigned __int64 a = (1 << 50);
>>    printf("%I64u\n", a);
>>
>>What will the result be?... 0 ! No errors, no warnings.
>
>I think the compiler sees the code as:
>
>int one = 1;
>__int64 a = one<<50;
>
>so the behavior is expected, no warning should be given IMO, there are plenty of
>cases where you deliberately want to over shift something to produce a zero.

I disagree.
You could claim that the compiler should never give a warning by this logic.

The behaviour is clearly not expected for the programmer who writes that
code(otherwise the programmer could write simply __int64 a=0;

>
>>After debugging for several hours I finally found the problem and fixed it
>>using:
>>
>>    unsigned __int64 a = (1ui64 << 50);
>>
>>Yes, 1 should be written as 1ui64, or another solution:
>>
>>    unsigned __int64 a = (((unsigned __int64) 1) << 50);
>>
>>Casting 1 to 64 bit.
>
>You must do the cast before the operation, ie. if you do something like:
>
>int x=...,y=...;
>__int64 f = x*y;
>
>it will not produce a __int64 result, similarly for a shift operation.

Yes and I think that it is a mistake in defining the standard C behaviour.

Uri



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.