Friday, February 8, 2008

Bit level programming:

Power of structure:

Bit field: There are number of questions which have only two answers. To store
such type of data there is necessity of only one bit either 0 or 1. Maximum child of any person must be less than 15. So for this four binary bit is sufficient to store such information. C introduces a powerful tool bit level programming in which we can store data which size can be in bit. So there is not necessity to declare int or char data type for only one or two bit data. In this way we can save the memory space.
Syntax:
struct
{
name1:bit_length;
name2:bit_length;
…………………………………………………………………………
…………………………………………………………………………
};

must be signed or unsigned int
If it signed int then minimum bit length must be two, one for sign and another for data.
Example
:
void main()
{
struct employee
{
unsigned id: 8;
unsigned sex:1;
unsigned age:7;
};
struct employee emp1={203,1,23};
clrscr();
printf("%d\t%d\t%d",
emp1.id,emp1.sex,emp1.age);
getch();
}
Output:
203 1 23
We can access the data member in same way.
How bit data is stored in the memory:
Minimum size of structure which data member in bit is two byte i.e 16 bit.This is called word size of microprocessor. Word size depends on microprocessor. Turbo c is based on 8086 microprocessor which word size is two byte.


Bits are filed in from right to left direction 8 bit for id,1 bit for sex and 7 bit for age.

pointer in bit level programming

Good program:
What will be output:
void main()
{
struct bitfield
{
unsigned a:5;
unsigned c:5;
unsigned b:6;

}bit;
char *p;
struct bitfield *ptr,bit1={1,3,3};
p=&bit1;
p++;
clrscr();
printf("%d",*p);
getch();
}
Output:
12
Explanation:
Binary value of a=1 is 00001 (in 5 bit)
Binary value of b=3 is 00011 (in 5 bit)
Binary value of c=3 is 000011 (in 6 bit)
In memory it is represented as:


Let address of bit1 is 500 which initialize to char pointer p. Since can is one byte data type so p++ will be 501. *p means content of memory location 501 which is (00001100) and its binary equivalent is 12. Hence output is 12.

Important point for bit level programming

1.
If the sum of total bit length is 16 or less than 16 then size of structure will be two byte and if is greater than 16 and less than equal to 32 then size of structure will be 4 byte and so on. Size must be multiple of two byte.
Program:
void main()
{
struct bitfield
{
signed int a:3;
unsigned int b:13;
unsigned int c:1;
};
struct bitfield bit1={2,14,1};
clrscr();
printf("%d",sizeof(bit1));
getch();
}
Output:
4
2.
Largest value can be stored in the n bit of bit length:
2^ (n-1)
If bit length is 4 then largest value it can store=2^3=8 otherwise its output will some difference.
3.We cannot take address of bit type variable so we can use scanf function.
4. There can be unused bit in word.
5. At same time member of structure can be normal data type (int,char,float,…) and bit type data.
Example:
void main()
{
struct bitfield
{
unsigned a:3;
char b;
unsigned c:5;
int d;
}bit;
clrscr();
printf("%d",sizeof(bit));
getch();
}
output:
5
Note: (Actual output will 6 due to slack byte ,So Before executing this program first go to option menu then compiler then code generation then select word alignment then press OK)

What is union?

UNION

union is user defined data type which stores different type of data in common memory location;
Syntax:
union []
Data type ;
Data type ;
………………………………………………………………..
………………………………………………………………….;
}[];

[ ] indicates they are optional;
Example :
void main()
{
union world
{
int a;
char b;
float c;
};
union world uk;
clrscr();
printf("%d",sizeof(uk));
getch();
}
Output:
4
Explanation:

Data member of union uses common(or share) memory space.Size of union is always a data member of union which has largest size. Here float c has largest size so size of union is 4.

In union only first member is active

Important point:
1.
In union only first member is active so we can initialize only first member. Rest member variable will assign a value which is store in share memory location.
Example:
void main()
{
union world
{
int a;
char b;
char c;
};
union world uk={5000};
char *ptr;
ptr=(char *)&uk;

clrscr();
printf("%d\t%d ",*ptr,uk.b);
getch();
}
Output :
-120 -120
Explanation:
Size of union will be 16 bit (int a has largest size which is 2 byte).Its active member is variable a, so a will assign value 5000.
Binary value of 5 is 1001110001000
which is represented in memory:

Let memory address of uk is 500 so ptr is pointing to the memory location 500. *ptr means content at location 500 which is 10001000.
Since its signed bit 1 so number is negative and negative number is stored in the memory in 2'scomplement format.

Decimal value of 1111000 is 120. Hence *ptr will -120. char b also uses same memory location i.e 500 So output of uk.b will be also -120.
1. Member of union can be union (not same), structure or array.
2. Nesting of union is possible i.e we can declare a union inside the other union.


0 comment here::