471,337 Members | 1,102 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Design, enums, and const ints

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
Dec 23 '05 #1
7 1436
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

Dec 23 '05 #2
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" <bi***********@hotmail.com> wrote in message
news:es*************@tk2msftngp13.phx.gbl...
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

Dec 23 '05 #3
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) [C# MVP]" <10*@100.com> wrote in message
news:ee**************@TK2MSFTNGP09.phx.gbl...
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" <bi***********@hotmail.com> wrote in message
news:es*************@tk2msftngp13.phx.gbl...
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


Dec 23 '05 #4
Bit Twiddler <bi***********@hotmail.com> wrote:
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 = ...;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 23 '05 #5
Bit Twiddler wrote:
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

Dec 24 '05 #6
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" <jm*****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Bit Twiddler wrote:
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

Dec 24 '05 #7
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

Dec 26 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by SpaceCowboy | last post: by
4 posts views Thread by David Rasmussen | last post: by
27 posts views Thread by Mark A. Gibbs | last post: by
6 posts views Thread by Noah Roberts | last post: by
4 posts views Thread by Craig | last post: by
4 posts views Thread by Martin Pritchard | last post: by
3 posts views Thread by lou zion | last post: by
2 posts views Thread by Simon Elliott | last post: by
7 posts views Thread by Victor Eijkhout | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.