BIT FIELDS
In C language, bits are manipulated in two ways. Those are:
1.
Implementation of Bit-wise Operators: C
language supports Bit-wise AND, Bit-wise OR, Bit-wise Exclusive-OR, One’s
complement, Left Shift and Right Shift operators as bit-wise operators that are
used for manipulating data at bit level of the operands.
2.
Utilization of bit fields within structure:
·
Bit
fields provides exact amount of bits required by the variable.
·
Bit
fields uses the memory very efficiently.
·
The
bits must be specified by non-negative(unsigned) integer type followed by a
colon(:).
·
Bit
fields can also used as member in unions.
The name and size of bit
fields are defined using structure. For
bit fields memory is allocated in adjacent bits. The general format of bit field definition
is:
Syntax: struct tag
{
unsigned
int member1 : bitwidth1;
unsigned
int member2 : bitwidth2;
-
- - - - -
-
- - - - -
unsigned
int membern : bitwidthn;
} variable;
/* Example C program - Bit Fields */
#include
<stdio.h>
struct
date
{
unsigned
int d:5;
unsigned
int m:4;
unsigned
int y:6;
}dt;
main()
{
dt.d=20;
dt.m=10;
dt.y=16;
printf("The
date is: %d/%d/%d",dt.d,dt.m,dt.y);
getch();
}
Output
The
date is: 20/10/16
No comments:
Post a Comment