You are on page 1of 22

Finishing School

Programme on C
Programming
BIT FIELDS

Mr. P. VEERA PRAKASH M.Tech.,

PROGRAMMING WITH C - BYRON GOTTFRIED


THIRD EDITION
BIT FIELDS IN STRUCTURE
 C facilitates the users to store integer members
in memory spaces smaller than what the
compiler would ordinarily allow.

 These space-saving structure members are


called bit fields.

 Bit fields are generally used in developing


application programs that must force a data
structure to correspond to a fixed hardware
representation and are unlikely to be portable.
BASIC CONCEPTS
 It is desirable to work with data items that consist
of only a few bits.
 1-bit flag - true/false
 3-bit integer - 0 to 7 value range
 7-bit ASCII character
 Such items are packed into an individual word of
memory.
 To do so, the word is divided into individual bit
fields.
 Bit Fields are defined as members of a structure,
and each bit field is accessed like any other
member of a structure.
General Syntax:
 In general, the decomposition of a word in
distinct bit fields can be written as

struct tag {
member1;
member2;
.
.
membern;
};
General Syntax:

 Each member declaration must include a


specification indicating the size of the
corresponding bit field.
 i.e., each member should be followed by a colon
and an unsigned integer indicating the field size.
 Syntax to declare a member:
type-specifier declarator: constant-expression;

 The interpretation of bit is compiler


dependent either left-right or right-left.
Example:
struct sample {
unsigned a:1;
unsigned b:3;
unsigned c:2;
unsigned d:1;
};
struct sample v;
where :
v.a is a field within v whose width is 1 bit,
v.b is a field within v whose width is 3 bits,
v.c is a field within v whose width is 2 bits,
v.d is a field within v whose width is 1 bit
Example:

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

| uncommitted bits | d| c | b |a|

Fig: Bit fields within a 16-bit word


Key points about bit fields:
 A bit field can only be defined as a portion of
an integer or an unsigned word.
 Some compilers permit a bit field to be a
portion of a char of long word.
 The rules for defining bit fields are the same
as the rules that govern other kinds of
structure
 They may appear with in arithmetic
expressions as unsigned integer quantities.
Key points about bit fields:

struct sample { struct {

unsigned a:1; unsigned a:1;

unsigned b:3; unsigned b:3;

unsigned c:2; unsigned c:2;

unsigned d:1; unsigned d:1;


}v; } v;
Key points about bit fields:
 A filed within a structure cannot overlap a word
within the computers memory.
#include<stdio.h>
main ( )
{
struct {
unsigned a : 5; /* begin first word */
unsigned b : 5;
unsigned c : 5;
unsigned d : 5; /* forced to second word */
} v = {1, 2, 3, 4};
printf ( “v.a=%d v.b=%d v.c=%d v.d=%d\n”, v.a, v.b, v.c, v.d);
printf (“v requires %d bytes \n”, sizeof(v));
}
Key points about bit fields:

word 2 word 1

bit no 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

| d | c | b | a |

Fig: Four bit fields within two 16-bit words

 Execution of the above program will produce the following output:


 v.a = 1 v.b = 2 v.c = 3v.d = 4
 v requires 4 bytes
Key points about bit fields:

 Unnamed fields can be used to control the


alignment of bit fields within a word of memory.

 Such fields provide padding within the word.

 The size of unnamed field determines the extent


of the padding.
Example of unnamed field:
#include<stdio.h>
main ( )
{
struct {
unsigned a : 5;
unsigned b : 5;
unsigned c : 5;
} v = {1, 2, 3);
printf ( “v.a=%d v.b=%d v.c=%d\n”, v.a, v.b, v.c);
printf (“v requires %d bytes \n”, sizeof(v));
}
 Execution of the above program will produce the following output:
 v.a = 1 v.b = 2 v.c = 3
 v requires 2 bytes
Example of unnamed field:
#include<stdio.h>
main ( )
{
struct {
unsigned a : 5; /* begin first word */
unsigned b : 5;
unsigned : 6; /* fill out first word*/
unsigned c : 5; /* begin second word */
} v = {1, 2, 3};
printf ( “v.a=%d v.b=%d v.c=%d \n”, v.a, v.b, v.c);
printf (“v requires %d bytes \n”, sizeof(v));
}
 Execution of the above program will produce the following output:
 v.a = 1 v.b = 2 v.c = 3
 v requires 4 bytes
Example of unnamed field:

word 2 word 1

bit no 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

| c | b | a |

Fig: Three bit fields within two 16-bit words


Key points about bit fields:
 Another way to control the alignment of bit fields
is to include an unnamed field whose width is
zero.
#include<stdio.h>
main ( )
{
struct {
unsigned a : 5; /* begin first word */
unsigned b : 5;
unsigned : 0; /* force alignment with second word*/
unsigned c : 5; /* begin second word */
} v = {1, 2, 3};
printf ( “v.a=%d v.b=%d v.c=%d \n”, v.a, v.b, v.c);
printf (“v requires %d bytes \n”, sizeof(v));
}
Key points about bit fields:
 Execution of the above program will produce the following output:
 v.a = 1 v.b = 2 v.c = 3
 v requires 4 bytes

word 2 word 1

bit no 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

| c | b | a |

Fig: Three bit fields within two 16-bit words


More about bit fields:

 Arrays of bit fields are not permitted


 The address-of operator (&) cannot be
applied to bit-field components. This
means that you cannot use scanf to read
values into a bit filed
 A pointer (*) cannot access a bit field;
 A function cannot return a bit field.
More about bit fields:
 C permits ordinary member variables along with
bit fields as structure members.

 The declarator is optional and is used to name


the bit field.

 Bit fields can only be declared as a part of a


structure.

 The address-of operator (&) cannot be applied to


bit-field components. This means that you
cannot use scanf to read values into a bit filed.
Drawbacks:
 First, the ordering of bits in memory is
architecture dependent and memory padding
rules vary from compiler to compiler.
Moreover, many C compilers that are used
today generate inefficient code for reading
and writing bit members.
 Second, bit fields can require a surprising
amount of runtime code to manipulate the
values and therefore, the programs may end
up using more space than they save.
Add-on:

 The minimum byte, which is usually machine


dependent is known as that word boundary.

 eg: TURBO C is based on the 8086


microprocessor and has two-byte word boundary.

You might also like