Page 1 of 1

Bit fields

Posted: Tue Jan 15, 2008 11:32 am
by neon
Hey everyone,

I have a quick question for the members in this community:

Do you prefer structs and bit masks over bit fields or not?

This is using C or C++, of course. I useually just use structs+bit masks to access individual bits, but am now coinsidering the use of bit fields instead as they dont require bit masks to access its members, and looks alot nicer as well.

This makes me wonder: What do you use in your OS?

Posted: Wed Jan 16, 2008 7:29 pm
by frank
When I have to be able to pick the order and packing of the bits I usually just go with a single byte or and integer. If I really just want to be able to set a single bit to on or off I usually use the bit fields. It all really depends on how I feel at the moment.

Posted: Thu Jan 17, 2008 12:10 am
by os64dev
Actually i wrote a valuetype base class with bit access and have int8-int64 and uint8-uint64 types derive from it. So i can use intValue(2,16) = -1; which would set the bits 2-18 or intValue to 1, however this is achieved by bitsmasks (to answer your question).

In OS programming a lot of the bits location, if not all, are fixed which makes very efficient code for my valuetype classes, but i never tested this with bit fields though.