By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,951 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

bit fileds or bit manuplation

P: n/a
hi,
i have to maintain a no of 1 bit flags.So i have two approaches
1) using bit fileds:
typedef struct flags_{
unsigned int f_1: 1;
unsigned int f_2: 2;
Jun 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
surendra wrote:
hi,
i have to maintain a no of 1 bit flags.So i have two approaches
1) using bit fileds:
typedef struct flags_{
unsigned int f_1: 1;
unsigned int f_2: 2;
.
.
.
} flags;
and then i can simply set or reset these flags by
flags myflags;
myflags.f_1 = 1;
myflag.f_2 = 0 ; and so on ..

2) the other approach is to take a simple variable and write some
macros to manuplate bits in it.
unsigned int flags;
#define SET(flags, i ) (flags | (0x01 <<i))
#define RESET(flags,i) (flags &~(0x01<<i))
so the question is which way i should go ?
Which ever you prefer, assuming the flags are just flags and don't map
to some external structure.
Is there any portability issue with bit fields.

Again, not unless the flags map to some external structure.

--
Ian Collins.
Jun 23 '06 #2

P: n/a
surendra wrote:

hi,
i have to maintain a no of 1 bit flags.So i have two approaches
1) using bit fileds:
typedef struct flags_{
unsigned int f_1: 1;
unsigned int f_2: 2;
.
.
.
} flags;
and then i can simply set or reset these flags by
flags myflags;
myflags.f_1 = 1;
myflag.f_2 = 0 ; and so on ..

2) the other approach is to take a simple variable and write some
macros to manuplate bits in it.
unsigned int flags;
#define SET(flags, i ) (flags | (0x01 <<i))
#define RESET(flags,i) (flags &~(0x01<<i))

so the question is which way i should go ?
I prefer the second.
Is there any portability issue with bit fields.


This is what K&R2 section 6.9 Bit-fields, says about masking:
"... these idioms are readily mastered ..."

This is what K&R2 section 6.9 Bit-fields says about bit-fields:
"Almost everything about fields is implementation-dependent"

--
pete
Jun 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.