-2

Being new to bitfields, I need some advice as to whats going on with various examples i've seen online. I'm wanting to use bitfields instead of bitmasks for readability, and maintenance ease later on for new ppl.

This is a common means of declaring a bitfield:

typedef enum 
{
   unsigned int x: 1;
   unsigned int y: 1;   
}statusBits1;

So far so good, then i've seen:

typedef enum 
{
   unsigned int x = 1 << 0,
   unsigned int y = 1 << 1   
}statusBits2;

In my learning, i believe this sets the default values for any statusBits2 datatypes. Does this also set the size of x and y to 1 bit fields like in statusBits1? A combination of both is what i'm looking for.

thanks in advance.

EDIT: Thank you for the answers! You forced me to reread what i've been studying. I was intermingling info from various posts regarding bitfields and bitmasks!

Namely these:

Declaring and checking/comparing (bitmask-)enums in Objective-C

bitwise indexing in C?

http://forum.codecall.net/topic/56591-bit-fields-flags-tutorial-with-example/

I'm coming back to C from a few years working in C#/C++, and relearning bit fiddling.

Community
  • 1
  • 1
  • 5
    are you sure you are defining an `enum`? where do you see this syntax? http://stackoverflow.com/questions/1102542/how-to-define-an-enumerated-type-enum-in-c – Jason Hu Jul 06 '15 at 14:39
  • From stack overflow: "Down-voting should be reserved for extreme cases." In hindsight, the question and my knowledge were lacking. add a comment and tell me how it was flawed, but downvoting is a not helpful. – iDriveTheLateCar Jul 07 '15 at 17:01
  • If it helps any, in this case I agree the downvotes aren't very helpful since they came without comments explaining why. But the vote to close I completely disagree with. It's obvious what you were asking. – Carey Gregory Jul 07 '15 at 20:26

2 Answers2

6

Your first example does not denote a bit field. This would be a struct instead of an enum.

In newer C++ you can define default values to struct members:

typedef struct
{
    unsigned int x = 1 << 0; /* Default value 1 << 0 = 1 */
    unsigned int y = 1 << 1; /* Default value 1 << 1 = 2 */
} statusBits2;

This is - however - not possible in standard C and older C++ standards and does not set the field width. You may also set the field width:

typedef struct
{
    unsigned int x : 1;   /* Field width 1 bit, no default */
    unsigned int y : 1;   /* Field width 1 bit, no default */
} statusBits2;

I don't know any way to define both default values and field width neither in C nor in C++.

Let me explain the meaning of your second example (as you wrote it, with enum):

Your second code almost denotes an enumeration. That is an integer type that can hold one of the denoted values. For example:

typedef enum
{
    fish = 3,
    gorilla = 7,
    rabbit = 9
} animal_t;

is a type, that can have one of the values 3, 7 and 9 which you can use by their names, e.g.:

animal_t my_animal = fish;

(in fact it can hold other integer values, too).

Both enums and structs can help to make your code clearer and more descriptive, but don't mix them up!

None of your examples is valid C or C++ code.

urzeit
  • 2,863
  • 20
  • 36
  • thanks for the help! "I don't know any way to define both default values and field width neither in C nor in C++" answered a nagging question i'd had. – iDriveTheLateCar Jul 07 '15 at 16:57
2

StatusBits2 doesn't contain bit fields at all. It contains two unsigned ints with default values 1 and 2. However, it's also not valid C. Bit fields require a struct not an enum.

Carey Gregory
  • 6,836
  • 2
  • 26
  • 47