C language referencec bit Fields
In addition to declarators for members of a structure or union,
Structure declarator can also be a specified number of BITs, called
"Bit Field." Its length is set off from the declarator FOR THE FIELD
Name by a colon. A bit field is interpreted as an integral type.
Syntax
-
Struct-declarator:
-
Declarator
Type-specifier declaratorOPT:Constant-expression
TheConstant-expressionSpecifies the width of the field in bits.Type-specifierFor the declarator must beUnsigned int,Signed int, OrInt, AndConstant-expressionMust be a nonnegative integer value. if the value is zero, the Declaration has no declarator. arrays of bit fields, pointers to bit fields, and functions returning bit fields are not allowed. the optional declarator names the bit field. bit fields can only be declared as part of a structure. the address-of operator (&) Cannot be applied to bit-field components.
Unnamed bit fields cannot be referenced, and their contents at Run
Time are unpredictable. They can be used as "Dummy" fields,
Alignment purposes. An unnamed bit field whose width is specified as 0
Guarantees that storage for the member following it inStruct-declaration-listBegins on an int boundary.
Bit fields must also be long enough to contain the bit pattern. For example, these two statements are not legal:
short a:17; /* Illegal! */
int long y:33; /* Illegal! */
This example defines a two-dimensional array of structures named screen.
struct
{
unsigned short icon : 8;
unsigned short color : 4;
unsigned short underline : 1;
unsigned short blink : 1;
} screen[25][80];
The array contains 2,000 elements. Each element is an individual structure containing four bit-field members: icon, color, underline, and blink. The size of each structure is two bytes.
Bit fields have the same semantics as the integer type. This means
Bit Field is used in expressions in exactly the same way as a variable
Of the same base type wocould be used, regardless of how many bits are in
The bit field.
Microsoft specific
Bit fields defined as int are treated as signed. A Microsoft extension to the ansi c standard allows char andLongTypes (bothSignedAnd unsigned) for bit fields. unnamed bit fields with base typeLong,Short, Or char (SignedOr unsigned) force alignment to a boundary appropriate to the base type.
Bit fields are allocated within an integer from least-significant to most-significant bit. In the following code
struct mybitfields
{
unsigned short a : 4;
unsigned short b : 5;
unsigned short c : 7;
} test;
int main( void );
{
test.a = 2;
test.b = 31;
test.c = 0;
}
The bits woshould be arranged as follows:
00000001 11110010
cccccccb bbbbaaaa
Since the 8086 family of processors stores the low byte of integer values before the high byte, the integer 0x01f2 abve wocould be stored in physical memory as 0xf2 followed by 0x01.
End Microsoft specific