"Bitmasks" are just bit patterns used in bitwise logic such as 0b1100.
You mean masks for the lowest N bits of a value, i.e. a specific type
of bitmask.
"
Your option 2 is very good! As well as being fast it works regardless
of word size, something the other two fail to do.
"
All four options require typecasting to the correct type in Delphi to
surpress "range checking and overflow checking (warnings/exceptions)".
So ultimately all methods will need to be adepted to their use...
When looking at it from that perspective perhaps the "constant" versions
are a bit more clear as to what the new typecast should be...
Maybe the typecast and the constants would make it clear to any viewing
programmer that they belong together...
Then again perhaps not
and then the other two "auto" options would
be better... but if the programmer would fail to add the correct
typecasts, then those would fail as well.
All would/will probably fail if typecast is not correctly modified,
however the constant versions require an additional modifcation.
Therefore the constants versions require additional work/effort to adept
them to new situations...
Thus from a time/programming efficiency view the "auto" versions would be
easier to use/require less time/less fiddling around with it
Though maybe that's not completely true... because option 2 the minus -1
version is actually harder to understand... and could actually lead to a
misunderstandig if the parenthesis weren't there...
It could be read by the programmer with the wrong precedence in mind for
example : 1 shl (BitCount-1) which would be wrong.
This problem is definetly not present with option 1 and option 3.
Though option 3 is probably also harder to understand because of
hexadecimals and especially the xor, which requires having the xor-table
in mind and working it out.
My constant version also requires recgonizing the constant value as being
"max word"... something I can do but maybe not a newby programmer.
Option 4 which uses "not 0" is also interesting... would a newby
understand it ?
Perhaps not because it could also be written without the parenthesis as
follows:
not word(not 0 shl BitCount);
Since not has a higher precedence in pascal at least then shl it's open to
interpretation by those not skilled in the precedence of operators.
It could also lead to translating issue's to other languages with
different "operator precedence".
Again the constant versions do not have this problem.
Option 3 is actually the most interesting when it comes to the
parenthesis... it probably doesn't need them at all:
It could be written as:
Mask := $FFFF shl BitCount xor $FFFF;
And it would still be correct, shl has a higher precedence than xor in
pascal...
Therefore option 3 seems most "idiot-proof"
Option 2 seems to be the worst for or-ing with something else for example:
mLongword :=
mLongword or
(
(1 shl mBitCount) - 1
);
^ This creates a range checking exception in Delphi, while the rest will
function happily for longwords... probably just Delphi specific but
still...
Hmmm... maybe it's better if it produces a range check error early on...
though the others seem stable with requiring a typecast but that could
change in the future if the delphi compiler is enhanced...
I think option 2, the minus -1 is a bit deceptive... it's easy to
remember, but also easy to miss-remember and get wrong probably.
The xor is a bit strange and not very intuitive.
Option 4 is a bit strange as well because it had two not's in it which is
kinda hard to understand... like what the hell is it doing ?
I shall introduce a new variant, version 5:
mLongword :=
mLongword or ( not (MaxLongword shl BitCount) );
This is not cool... because I don't know the value of max longword out of
my head... so that would require a calculator... at least if I wanted to
write
it in decimal... I actually did not want to do that... I wanted to do it
in hexadecimal so here goes again:
version 6:
mLongword :=
mLongword or ( not ($FFFFFFFF shl mBitCount) );
I think this one is best for now. It also doesn't require a typecast which
is nice !
So I guess we didn't have all versions yet after all !
slight
variations ofcourse... but this slight variation does matter for me !
So for now I will use version 6.