Design, enums, and const ints

B

Bit Twiddler

I have a home grown bit vector and I keep oscillating between what I use
access individual bits.

For example, is is better to do this:

const int BIT_1 = 1;
const int BIT_2 = 2;
....

int my_bits;

// Set bit 1
my_bits |= BIT_1;

Or, should I create an enum

enum BitPos
{
Bit1 = 1,
Bit2 = 2,
....
}

// Set bit 1
my_bits |= (int)BitPos.Bit1;

--------------------------------------------------------

The explicit cast bothers me... any opinions as to how you might do this
would be appreciated!

-Reed
 
J

Jesse McGrew

Why not just make my_bits a BitPos variable instead of an int? Then you
won't need to cast every time you set or unset a bit.

Jesse
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi,

Instead of using *enum* create a class with constants or static readonly
fields or properties of type *int* this way you don't have to cast.
 
B

Bit Twiddler

Thanks both to Jesse and Stoitcho for their responses.

I went with the static readonly field method (Jesse, they can't be simple
variables because their values must *not* change after they are
initialized).

In my specific case, object size is incredibly important, so creating
wrapper classes, etc. would incur too much memory overhead.

Thanks,
BT


Stoitcho Goutsev (100) said:
Hi,

Instead of using *enum* create a class with constants or static readonly
fields or properties of type *int* this way you don't have to cast.


--

Stoitcho Goutsev (100) [C# MVP]

Bit Twiddler said:
I have a home grown bit vector and I keep oscillating between what I use
access individual bits.

For example, is is better to do this:

const int BIT_1 = 1;
const int BIT_2 = 2;
...

int my_bits;

// Set bit 1
my_bits |= BIT_1;

Or, should I create an enum

enum BitPos
{
Bit1 = 1,
Bit2 = 2,
...
}

// Set bit 1
my_bits |= (int)BitPos.Bit1;

--------------------------------------------------------

The explicit cast bothers me... any opinions as to how you might do this
would be appreciated!

-Reed
 
J

Jon Skeet [C# MVP]

Bit Twiddler said:
Thanks both to Jesse and Stoitcho for their responses.

I went with the static readonly field method (Jesse, they can't be simple
variables because their values must *not* change after they are
initialized).

I think you missed Jesse's point - he was suggesting that the variable
should be of the enum type rather than an int. In other words:

static readonly BitPos my_pos = ...;
 
J

Jesse McGrew

Bit said:
I went with the static readonly field method (Jesse, they can't be simple
variables because their values must *not* change after they are
initialized).

As Jon mentioned, you seem to have misunderstood me. I'm suggesting
something like this:

// declare an enum to hold the bit values
// the Flags attribute indicates that multiple values can be combined
[Flags]
enum BitPos {
None = 0,
Bit1 = 1,
Bit2 = 2,
Bit3 = 4,
// etc.
}

public void BitFieldDemo() {
// here is your bitfield
BitPos my_bits = BitPos.None;

// now let's set a bit
my_bits |= BitPos.Bit1;

// let's see if another bit is set
if ((my_bits & BitPos.Bit2) != 0)
Console.WriteLine("bit 2 is set - how did that happen?");
else
Console.WriteLine("good, bit 2 is not set");
}

Now you won't have to cast when you set, unset, or test bits. You will
have to cast if you need to convert my_bits to/from an int, though. One
benefit of using a [Flags] enum is you can call my_bits.ToString() to
get a human-readable string like "Bit1, Bit3" instead of just a number.

Jesse
 
B

Bit Twiddler

Thanks Jesse (and Jon). I *did* misunderstand you Jesse - thanks for the
clarification.

Let's say that I am using a BitVector32 to store my bits. The bits in a
BitVector32 are indexed like an array.

Since I have to use an int to index into the BitVector32 I need to create
things like this:

private static readonly int BIT_1 = BitVector32.CreateMask(); //
1st bit
private static readonly int BIT_2 = BitVector32.CreateMask(BIT_1); // 2nd
bit

I'm not a fan of static ints like that so I wanted to do something like
this:

private enum BitPos
{
Bit1 = 1.
Bit2,
....
}

But if I do that, and I want to index into the BitVector32 a cast is
required:

my_bitvector[(int)BitPos.Bit1]; // get the value of the 1st bit

It appears that the generated byte code is identical - thus the explict cast
is not a performance hit.

Any thoughts would be appreciated!

BT


Jesse McGrew said:
Bit said:
I went with the static readonly field method (Jesse, they can't be simple
variables because their values must *not* change after they are
initialized).

As Jon mentioned, you seem to have misunderstood me. I'm suggesting
something like this:

// declare an enum to hold the bit values
// the Flags attribute indicates that multiple values can be combined
[Flags]
enum BitPos {
None = 0,
Bit1 = 1,
Bit2 = 2,
Bit3 = 4,
// etc.
}

public void BitFieldDemo() {
// here is your bitfield
BitPos my_bits = BitPos.None;

// now let's set a bit
my_bits |= BitPos.Bit1;

// let's see if another bit is set
if ((my_bits & BitPos.Bit2) != 0)
Console.WriteLine("bit 2 is set - how did that happen?");
else
Console.WriteLine("good, bit 2 is not set");
}

Now you won't have to cast when you set, unset, or test bits. You will
have to cast if you need to convert my_bits to/from an int, though. One
benefit of using a [Flags] enum is you can call my_bits.ToString() to
get a human-readable string like "Bit1, Bit3" instead of just a number.

Jesse
 
J

Jesse McGrew

I've never encountered the BitVector32 class before, but after skimming
the documentation for it, my suggestion is to ditch it and just store
your bits in a BitPos variable. BitVector32 doesn't really seem to do
much other than wrapping the &, |, and << operators, unless you're
using it in "section" mode.

Furthermore, since enums can be based on any integer type, they can
store 8, 16, 32, or 64 bits, while a BitVector32 is always 32 bits
wide. For example, to define an enum that can hold 64 bits, start the
definition with "enum BitPos : long" instead of just "enum BitPos".

If you need to use BitVector32, for example to hide the details of
bitwise operations from inexperienced programmers who might read your
code, then I'd suggest storing the masks in static readonly int fields.
I wouldn't use constants, because that seems like an unnecessary mixing
of the two worlds - if you're going to hardcode the masks yourself
(rather than using CreateMask), why not go all the way and do the
bitwise operations yourself too?

Jesse
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top