Skip to contentSkip to conversation

Bitfields

DISCUSSION Jagadeesh Bhaskar P 2 participants 2 messages Dec 1, 2004

lore
Share & tools
lore
#0 lore raw

Hi all,
What is a bitfield? How is it used in C??

TIA

Signature · 3 lines
With regards,

Jagadeesh Bhaskar P
#1 lore raw
Quoted 4 lines · Jagadeesh Bhaskar P

Hi all,
What is a bitfield? How is it used in C??

TIA

It is possible to use several of bit variables like fields within a struct. You have to indicate first the name of the field, then the number of bits that the field occupies. Also you have to put double points ":" between the filed's name and its bit-size.
For example, you want to save the current date; you can define a struct Date (YYYY/MM/DD) like this

struct Date { // Date format: YYYY/MM/DD
unsigned year : 7; // 0-127 (2000-2127) [7 bits size]
unsigned month : 4; // 1-12 [4 bits size]
unsigned day : 5; // 1-31 [5 bits size]
};

In this case we have the complete date in a 16 bit variable (7+4+5=16 bit)!

Signature · 3 lines
With regards,
				HIToC 
				[email protected]