Bit fields

Programming, for all ages and all languages.
Post Reply
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Bit fields

Post 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?
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post 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.
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post 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.
Author of COBOS
Post Reply