Computer Chess Club Archives


Search

Terms

Messages

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

Author: Sune Fischer

Date: 10:15:59 02/08/04

Go up one level in this thread


On February 08, 2004 at 13:06:00, Uri Blass wrote:

>On February 08, 2004 at 12:55:54, Sune Fischer wrote:
>
>>On February 08, 2004 at 12:05:12, Dieter Buerssner wrote:
>>
>>>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.
>>>
>>>You cannot do this in Standard C.
>>>
>>>6.5.7 Bitwise shift operators
>>>
>>>[...] If the value of the right operand is negative or is
>>>greater than or equal to the width of the promoted left operand, the behavior is
>>>undefined.
>>
>>I knew about the negative shift but not the latter.
>>I wonder why that is the case, I see no reason why it should be undefined.
>>
>>>A warning would be useful anyway for a constant expression. Gcc: left shift
>>>count >= width of type.
>>
>>It's just a way of writing a constant, ie. would you also want a warning on 1<<2
>>saying that constant 1 being altered to 4?
>>
>>The compilers intent would be to tell you that if you want 4 and not 1, why not
>>just write 4 instead of 1?
>
>This is different
>Here 2^2=4 and 2^50!=0
>
>Warning that 1<<50 is not 2^50 is a clearly useful warning.

I agree it should produce a warning, not for the reason you give but because if
Dieter is correct that it is undefined behavior then a warning would
(definitely) be in order.

-S.
>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.