473,320 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1526
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
4
by: David Rasmussen | last post by:
The problem comes up in all sorts of contexts. But here is an example: We want to model the basics of chess, maybe for making a chess program, or maybe just to make an interactive board or ... ...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
6
by: Noah Roberts | last post by:
I am having an interesting compilation error that makes me wonder if enums can conflict with each other. For example: class A { enum X { TEST, TEST2 }; public: ....
4
by: Craig | last post by:
Newbie... I'm casting enums back and forth to ints so to use them as an index into a complex two dimensional array. I thought it would help readability. Instead, my code has casts all over the...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
3
by: lou zion | last post by:
hi all, i'm trying to use a classes enums in another class, but can't seem to find the right syntax. i've got class A which has: enum EditTypeA {Currency, Percent, LabeledNumber, Text};...
2
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default,...
7
by: Victor Eijkhout | last post by:
Intel icc seems to think that enums are not ints: #include <stdlib.h> typedef enum { TRUE=1, FALSE=0 } Truth; #undef __FUNCT__ #define __FUNCT__ "getcontrols" int getcontrols(Truth...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.