Author: Dieter Buerssner
Date: 13:06:06 12/05/02
Go up one level in this thread
On December 05, 2002 at 15:41:02, Gerd Isenberg wrote: >On December 05, 2002 at 12:48:02, Dieter Buerssner wrote: > >>On December 05, 2002 at 11:25:27, Gerd Isenberg wrote: >> >>>Most (All?) C-compilers have no problem with unary minus and constants: >>> >>>unsigned int A = -CONST; >> >>Assume 32 bit integers, and const is -2^31. >>I believe, the above is undefined behaviour. In this case a cast will make it >>defined behaviour: >> >> unsigned int A = -(unsigned)CONST. >> > >Hi Dieter, Hi Gerd, I just was pedantic :-) >I guess it's null, because 2^31 is INT_MIN. I had small constants in mind. >So -1 is very common to to set all bits in an unsigned variable, don't care >about the word size. > >unsigned int A = -1; Yes, this is guaranteed to work. >if ( popCount(A) == 64) >{ > // ah, nice native 64 bit ints >} > >Where i have some problems with sometimes, is the implicite type of a direct >constant expressions, calculated by the compiler, specially with propritary >64 bit integers types. > >Eg.: MSC6.0 > >unsigned __int64 FFFF = -1; // that' fine, all 64 bits set >unsigned __int64 H8BB = 1<<63; // oups zero > >produces an internal unsigned int overflow and assigns zero( without any >warning, even with warning level 4). Why isn't the compiler able to cast the 1 >implicitly to the 64bit type? If we speak about __int64, we leave Standard ISO C. But assume it were unsigned long long instead (then it is Standard C again, the same argument holds for smaller types). The compiler would not be Standard conforming, if it implicetely casted the 1 to unsigned. The right hand side must be evaluated as a signed int (and not as a signed long long). unsigned long a = 1<<31; will probably also not give the expected result. unsigned long a = 1UL<<31; would "fix" this. >>>Because -CONST is a kind synonym for the compiler which means implicitly >>> (2**wordLengthInBits) - CONST ==> 0 - CONST >> >>All unsigned arithmetics is guaranteed to yield results mod 2 ^ bits. > >except imul and idiv ;-) My comment was in the context of Standard C. There is no imul in C. See you in Paderborn, 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.