From: Skybuck Flying on 25 May 2010 09:35 The other methods have problems too ofcourse, that's why I added the "word" typecasts... though when leaving out the "word" typecast not all problems are detected by Delphi's range checking capabilities. Therefore it's worth bitching about Delphi not being able to detect problems: > 1. (Skybuck) Mask := not word(65535 shl BitCount); // not 1111000 = > 0000111 This method without the typecast should have raised an exception since 65535 is being shifted out and becomes to large for a word... Therefore in 16 bit it will work but in 32 bit it will still bug out, a typecast for longword will fix it again. All methods have this problem, therefore all methods require a typecast to be on the safe side if range/overflow checking is to be enabled. > > 2. (Michael Vinther) Mask := (1 shl BitCount)-1; // 10000-1 = 09999 = > 01111 ;) :) > > 3. (Skamradt) (Mask := ($FFFF shl BitCount) xor $FFFF; // 1111000 xor > 1111111 = 0000111 > > 4. (MitchAlsup) Mask := not word((not 0) shl BitCount); // not((not 0 = > 1111111) shl 3 = 1111000) = 0000111 Moral of the story: Code designed will work in 16 bit in Delphi but would present problems in 32 bit. The typecasts were added because I already had experience with 32 bit problems/versions. Bye, Skybuck.
From: Skybuck Flying on 25 May 2010 09:40 Wow when the bit count changes from 16 to 15 then suddenly my method starts to trip range checking exceptions as well ! Totally funny ! =D This is some fun code we have here ladies and gentlemen ! ;) :) Bye, Skybuck =D
From: Skybuck Flying on 25 May 2010 09:42 Wow method 1 even trips for a BitCount of 0 ?!? How is that possible ? Seems like Delphi compiler has some real issue's with this code and it's range checking capabilities ?! :) Bye, Skybuck.
From: Skybuck Flying on 25 May 2010 09:43 Method 3 does work for BitCount of 0 and starts to trip at BitCount 1 ! Most amuzing ! =D Bye, Skybuck =D
From: Skybuck Flying on 25 May 2010 09:50
I guess the moral of this story is: Delphi's Overflow/Range checking cannot be trusted at all... I already knew it was bad and missed some cases... but this is getting pretty ridicules! ;) :) It even detects cases/"fake bugs" which seem totally legal and therefore shouldn't have been detected at all ?! wow ?! ;) :) None-the-less it always helps having it on... but it should not be relied on to detect everything. Bye, Skybuck =D |