Yes...each bit set in the InvalidBits value is a bit that does not appear
in any of the valid values. Thus, if one or more of those bits are set,
you've got an invalid value.
In my example, the programmer is required to maintain the value
explicitly. There's zero performance penalty, and it's a lot less code,
but it does have the maintenance hazard I mentioned earlier.
The other approach I mentioned is elaborated on by Arne and Ben. Ben's
example sure is fancy
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
, but I find Arne's more readable (if you fix the
little mistake about the final comparison against "val" versus "0").
I admit, I don't really understand why Ben's example goes to so much
trouble, what with messing around with the expressiont tree and all. Even
if I were going to use the Enumerable.Aggregate() method, I think I would
just use it in the conventional way, simply providing a delegate that or's
the previous value with the current value. But knowing Ben, I'm sure
there's a method to his madness.