bit field initialization

D

David Rubin

Can bit fields be initialized with an initializer list, particularly if they are
fields of a structure?

struct {
int a : 1;
int b : 3;
int c : 5;
int d : 7;
} foo = {0, 2, 30, 120};

/david
 
E

Eric Sosman

David said:
Can bit fields be initialized with an initializer list, particularly if they are
fields of a structure?

struct {
int a : 1;
int b : 3;
int c : 5;
int d : 7;
} foo = {0, 2, 30, 120};

Yes. However, note that it's implementation-defined
whether a plain `int' bit-field is signed or unsigned, so
the initializers for `c' and `d' may be too large. For
the same reason, `a' might be incapable of holding any
value other than zero.

If you want to use bit fields (which, IMHO, are less
useful than their appearance suggests), I'd recommend
writing `unsigned int' or `signed int' explicitly instead
of trusting to the implementations' whims.
 

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

Members online

Forum statistics

Threads
474,141
Messages
2,570,815
Members
47,361
Latest member
RogerDuabe

Latest Threads

Top