Author: Sune Fischer
Date: 09:55:54 02/08/04
Go up one level in this thread
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?
That would be a legal warning too then, still extremely pedantic.
>>>This is just a stupidity of MSVC. I was told that the Intel compiler gives an
>>>appropriate warning for the statement (1 << 50), and handles it correctly as 64
>>>bit.
>>
>>I can't imagine it would do that, I don't think it would be correct according to
>>the standard.
>
>First of all, when you are using __int64, you have left Standard C.
What I mean is the "1" is of standard type DWORD, ie. 4 bytes, so it could never
by any standard C operation become an 8 byte valued.
A 64 bit compiler might treat "1" as a __int64 by default, not sure what the
"ANSI" should be on that if it ever gets formally extended to include 64 bit
variables.
>Second, a
>Standard compiler can give as many warnings as it likes. There are some compiler
>diagnostics, that are required for a Standard conforming compiler, but non are
>disallowed.
>
>MSVC gives warnings for overflow of constant expressions (but not in this case).
Note this is different from the shift example.
The constant is 1<<50 which gets saved to a __int64, so there is no overflow and
therefor no warning.
>It gives many warnings, I find annoying (for example a warning for "while(1){/*
>With or without break inside */}"). A warning for 1<<52 would be more useful,
>IMHO.
Most of the warnings are a bit on the pedantic side IMO, I usually do a
for(;;) instead of while(1), both of them needs a break anyway but the for just
"looks cleaner" to me without the branch and I don't get a stupid warning.
-S.
>Regards,
>Dieter
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.