R
Richard Dixson
I have a need for a variable to hold a bitwise mask that can exceed 64
possible values. What is the best way to do this using C#?
For example, currently I have a variable called optionsMask that is defined
as a Long. Then I set the constants like this:
public const long BLACK = 0L; // 1st
public const long GREEN = 1L; // 2nd
public const long RED = 2L; // 3rd
public const long BLUE = 4L; // 4th
public const long PURPLE = 8L; // 5th
public const long PINK = 16L; // 6th
public const long GRAYBLUE = 32L; // 7th
...
public const long HONEYDUE = 4611686018427387904L; // 64th, max
possible
Then in my code I am doing things like: long optionsMast = BLACK | RED |
HONEYDUE;
Also other developers in my group set the bitmask in their code and pass it
into my library via a setter (if this matters).
Now of course the challenge is that I have run out of values for the mask at
64! If I change to use a ulong that gets me one more value
(9223372036854775808), but on the next one (18446744073709551616) I of
course get the error "Integral constant is too large".
So this brings me back to my main question - what is the best approach to be
able to use a single variable to store combinations of flags that go beyond
64?
I realize I could start another mask, like optionsMask2, but that would get
very messy in the code (having to check two different variables for set
bits, plus developers would have to know which bits to set on which mask
variable - this would be very poor I think).
For performance reasons it is very important that I do this using some sort
of numeric representation that can be tested to see what bits are set in the
mask very quickly. For example the current approach works great, except its
limited to 64 combinations. For instance I could not use an approach that
used strings like "black, green, blue " and then seached in the string for
values like ", green" to see if there was a match.
I am using C#. Ideally I would like whatever new approach is needed to
solve this issue to also work with the .NET framework 1.0. However if there
is a much more elegant version in 1.1 I would consider it.
Can someone please let me know if there is some sort of construct that would
enable me to create a bitwise mask (or something equal in performance) for
combining more than 64 combinations? If it matters, this is something that
my developers use outside via a setting (for example they currently build
the mask in their C# code and pass it in via a setter property in my code
library). Ideally the approach would not be limit future expansion to
another max ceiling number; however an approach that gets me about another
20 or 30 options should do ok for a while at least.
Thank you very much in advance,
Richard
possible values. What is the best way to do this using C#?
For example, currently I have a variable called optionsMask that is defined
as a Long. Then I set the constants like this:
public const long BLACK = 0L; // 1st
public const long GREEN = 1L; // 2nd
public const long RED = 2L; // 3rd
public const long BLUE = 4L; // 4th
public const long PURPLE = 8L; // 5th
public const long PINK = 16L; // 6th
public const long GRAYBLUE = 32L; // 7th
...
public const long HONEYDUE = 4611686018427387904L; // 64th, max
possible

Then in my code I am doing things like: long optionsMast = BLACK | RED |
HONEYDUE;
Also other developers in my group set the bitmask in their code and pass it
into my library via a setter (if this matters).
Now of course the challenge is that I have run out of values for the mask at
64! If I change to use a ulong that gets me one more value
(9223372036854775808), but on the next one (18446744073709551616) I of
course get the error "Integral constant is too large".
So this brings me back to my main question - what is the best approach to be
able to use a single variable to store combinations of flags that go beyond
64?
I realize I could start another mask, like optionsMask2, but that would get
very messy in the code (having to check two different variables for set
bits, plus developers would have to know which bits to set on which mask
variable - this would be very poor I think).
For performance reasons it is very important that I do this using some sort
of numeric representation that can be tested to see what bits are set in the
mask very quickly. For example the current approach works great, except its
limited to 64 combinations. For instance I could not use an approach that
used strings like "black, green, blue " and then seached in the string for
values like ", green" to see if there was a match.
I am using C#. Ideally I would like whatever new approach is needed to
solve this issue to also work with the .NET framework 1.0. However if there
is a much more elegant version in 1.1 I would consider it.
Can someone please let me know if there is some sort of construct that would
enable me to create a bitwise mask (or something equal in performance) for
combining more than 64 combinations? If it matters, this is something that
my developers use outside via a setting (for example they currently build
the mask in their C# code and pass it in via a setter property in my code
library). Ideally the approach would not be limit future expansion to
another max ceiling number; however an approach that gets me about another
20 or 30 options should do ok for a while at least.
Thank you very much in advance,
Richard