Structure and Union 397
Record N o.: 2
First Name : Vijay
Middle Name : Kamal
Last Name : Nandedkar
City & Pincode : Nanded - 431 602
Record No. : 1
First Name : Jay
Middle Name : Mohan
Last Name : Deshmukh
City & Pincode : Nanded - 431 602
Record No. : 2
First Name Vijay
Middle Name : Kamal
Last Name Nandedkar
City & Pincode : Nanded - 431 602
Explanation In the above program the typede f structure contains employee details like first, middle,
last name, city & pincode. Based on this structure the user defines data type name & which is further
used in the program to accept and print the relevant data fed by the user.
12.9 BITFIELDS
Bit field provides exact amount of bits required for storage of values. If a variable value is 1 or 0 we need
a single bit to store it. In the same way if the variable is expressed between 0 and 3, then the two bits are
sufficient for storing these values. Similarly if a variable assumes values between 0 and 7 then three
bits will be sufficient to hold the variable and so on. The number of bits required for a variable is
specified by non-negative integer followed by colon.
To hold the information we use the variables. The variables occupy a minimum of one byte for
char and two bytes for integer. Instead of using complete integer if bits are used, space of memory
can be saved. For example, to know the information about the vehicles, following information has to
be stored in the memory.
1. Petrol Vehicle
2. Diesel Vehicle
3. Two Wheeler Vehicle
4. Four Wheeler Vehicle
5. Old Model
6. New. Model
In order to store the status of the above information we may need two bits for types of fuel as to
whether the vehicle is of petrol or diesel type. Three bits for its type as to whether the vehicle is two
or four-wheeler. Similarly, three bits for model of the vehicle. Total bits required for storing the information
would be 8 bits i.e. 1 byte. It means that the total information can be packed into a single byte. Eventually
bit fields are used for conserving the memory. The amount of memory saved by using bit fields will be
substantial which is proved from the above example.
However there are restrictions on bit fields when arrays are used. Arrays of bit fields are not
permitted. Also the pointer cannot be used for addressing the bit field directly, although the use of
398 Programming and Data Structures
the member access operator (->) is acceptable.
The unnamed bit fields could be used for padding as well as for alignment purposes.
The structure for the above problem would be as follows,
struct vehicle
{
unsigned type: 3;
unsigned fu e l: 2;
unsigned model: 3;
} ;
The colon (:) in the above declaration tells to the compiler that bit fields are used in the structure and
the number after it indicates how many bits are required to allot to the field. Simple programs are
illustrated below.
12.21 Write a program to store the information of vehicles. Use bit fields to store the status
of information.
# Include <stdio.h>
# include <con±o.h>
# define PETROL 1
# define DISEL 2
# def ine TWO WH 3
# define FOORWH 4
# define OLD 5
# defin e NEW 6
void mainO
{
struct vehicle
{
unsigned type : 3;
unsigned fuel : 2;
unsigned model :3;
}>
struct ve hicle v;
v. t^pe=*FOUR_WH;
v.fuel*DISEL;
v.model=OLD;
clrscrO ;
printf ("n Type o f Vehicle: %d",v.type);
printf (" n Fuel : % d",v.fuel);
printf ( " M odel : % d",v.model);
I
Structure and Union 399
QirrPUT;
Type of Vehicle : 4
Fuel : 2
Model : 5
Explanation In the above program using # define macros are declared. The information about
the vehicle is indicated between integers 1 to 6. The structure vehicle is declared with bitfields. The
number of bits required for each member is initialized. As per the program, for type of vehicle requires
3 bits,fuel requires 2 bits and model requires 3 bits. An object v is declared. Using the object bits fields are
initialized with data. The output of the program displays integer value stored in the bit fields, which
can be verified with macro definitions initialized at the beginning of the program.
12.22 Write a program to display the examination result of the student using bit fields.
# include <std io.h>
# include <conio.h>
# defin e PASS 1
# define FAIL 0
# define A 0
# define B 1
# define C 2
void main ()
{
struct student
{
char *name;
unsigned re su lt : 1;
unsigned grade : 2;
}>
struct student v;
v . names"Sachin";
v . result=PASS;
v.grade =C;
c l r s c r ()?
printf (" n Name :% s",v.name);
printf (" n R e sult: %d",v.result);
printf C'n Grade : % d ”,v.grade);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.14.93