From: Skybuck Flying on
However Michael's Code is a bit dangerous because of the way Delphi compiles
it:

At first glance during testing apperently it doesn't require a "word"
typecast to make it safe:

function Mask2MichaelVinther( ParaBitCount : byte ) : word; inline;
begin
result := (1 shl ParaBitCount)-1;
end;

Carefully looking at this function makes it suspicious

1 shl 16 would become 0.

0 - 1 should trigger a range checking exception... but the exception never
occurs.

Even when range+overflow checking is on and inlining is off the code doesn't
trip:

It seems to be a Delphi 2007 compiler range/overflow checking bug not being
able to detect this scenerio:

"
TestProgram.dpr.40: result := (1 shl ParaBitCount)-1;
00408E4C 8BC8 mov ecx,eax
00408E4E B801000000 mov eax,$00000001
00408E53 D3E0 shl eax,cl
00408E55 83E801 sub eax,$01
00408E58 7105 jno $00408e5f
00408E5A E851ADFFFF call @IntOver
00408E5F 3DFFFF0000 cmp eax,$0000ffff // probably bug here... eax will
be $FFFF FFFF

Unable to determine from intel documentation what cmp and jbe do...
is it A <= B ? or B <= A ?

00408E64 7605 jbe $00408e6b
00408E66 E83DADFFFF call @BoundErr
TestProgram.dpr.41: end;
00408E6B C3 ret
"

Bye,
Skybuck.