Section 2: Compiler
65
TI
-
89 / TI
-
92 Plus Sierra C Assembler Reference Manual
Not for Distribution
Beta Version February 2, 2001
As previously stated, enumeration constants and variables can be used anywhere
integer types can be used except in assignments involving different enumeration
types. The following examples utilize the enumeration objects declared on the
previous page to demonstrate enum type checking across assignments:
int i;
CA = sunny;
/* legal
*/
i = 25;
/* legal
*/
red = 36;
/* illegal, red is constant
*/
car = boat;
/* legal
*/
car = NJ;
/* illegal
*/
FL = green;
/* illegal
*/
boat = i;
/* legal
*/
2.9.6.
Bit Field Description
The C language allows integer data to be stored in spaces that differ in size from
those provided by the basic integer types. Such arbitrarily sized integers are
called bit fields. They are supported by allowing the width in bits of a structure or
union member to be specified using a colon and a constant expression after the
member declarator. In the following declaration, x.a is a 10-bit integer, x.b is a
12-bit integer, and x.c is a seven-bit integer:
struct s {
long int a:10;
long int b:12, c:7;
} x;
The three members of this structure occupy a total of four bytes; internally, they
are packed into a space that has the same size and alignment as a long int.
Bit fields can be packed in any of the four basic integer types (for example, char,
short int, int, or long int). The size of the type determines the maximum
allowable bit field size. Bit field declarations can also include the signed and
unsigned type specifiers. The signed type specifier causes the bit field to be
interpreted as a signed quantity — the most significant bit of the bit field
(for example, the sign bit) is extended when extracting the contents of the field.
The unsigned type specifier can also be used, but has no effect since bit fields
are unsigned by default. Given the following initialization, the three bit field
members will have the values shown:
struct t {
int i:3;
signed int j:3;
unsigned int k:3;
} y = { -1, -1, -1 };
/* -1 bit pattern all 1's */
y.i = 7
y.j = -1
y.k = 7