Bit Fields in C
Bit Fields in C
Bit Fields in C
Bit Fields in C
In C, we can specify size (in bits) of structure and union
members. The idea is to use memory efficiently when we
know that the value of a field or group of fields will never
exceed a limit or is withing a small range.
For example, consider the following declaration of date
without use of bit fields.
#include <stdio.h>
// A simple representation of
date
struct date
{
unsigned int d;
unsigned int m;
unsigned int y;
};
int main()
{
printf("Size of date is %d
bytes\n", sizeof(struct date));
struct date dt = {31, 12, 2014};
printf("Date is %d/%d/%d",
dt.d, dt.m, dt.y);
}
Output:
Size of date is 12 bytes
Date is 31/12/2014
The above representation of ‘date’ takes 12 bytes on a
compiler where an unsigned int takes 4 bytes. Since we
know that the value of d is always from 1 to 31, value of
m is from 1 to 12, we can optimize the space using bit
fields.
#include <stdio.h>
// A space optimized
representation of date
struct date
{
// d has value between 1 and
31, so 5 bits
// are sufficient
unsigned int d: 5;
// m has value between 1 and
12, so 4 bits
// are sufficient
unsigned int m: 4;
unsigned int y;
};
int main()
{
printf("Size of date is %d
bytes\n", sizeof(struct date));
struct date dt = {31, 12, 2014};
printf("Date is %d/%d/%d",
dt.d, dt.m, dt.y);
return 0;
}
Output:
Size of date is 8 bytes
Date is 31/12/2014
Following are some interesting facts about bit fields in
C.
1) A special unnamed bit field of size 0 is used to force
alignment on next boundary. For example consider the
following program.
#include <stdio.h>
// A structure without forced
alignment
struct test1
{
unsigned int x: 5;
unsigned int y: 8;
};
// A structure with forced
alignment
struct test2
{
unsigned int x: 5;
unsigned int: 0;
unsigned int y: 8;
};
int main()
{
printf("Size of test1 is %d
bytes\n", sizeof(struct test1));
printf("Size of test2 is %d
bytes\n", sizeof(struct test2));
return 0;
}
Output:
Size of test1 is 4 bytes
Size of test2 is 8 bytes
2) We cannot have pointers to bit field members as they
may not start at a byte boundary.
#include <stdio.h>
struct test
{
unsigned int x: 5;
unsigned int y: 5;
unsigned int z;
};
int main()
{
struct test t;
// Uncommenting the
following line will make
// the program compile
and run
printf("Address of t.x is
%p", &t.x);
// The below line works
fine as z is not a
// bit field member
printf("Address of t.z is
%p", &t.z);
return 0;
}
Output:
error: attempt to take address of bit-field structure
member 'test::x'
3) It is implementation defined to assign an out-of-range
value to a bit field member.
#include <stdio.h>
struct test
{
unsigned int x: 2;
unsigned int y: 2;
unsigned int z: 2;
};
int main()
{
struct test t;
t.x = 5;
printf("%d", t.x);
return 0;
}
Output:
Implementation-Dependent
EXAMPLE PROGRAM – PASSING STRUCTURE TO
FUNCTION IN C BY VALUE:
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student record);
int main()
{
struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(record);
return 0;
}
void func(struct student record)
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}
https://fresh2refresh.com/c-programming/c-passing-
struct-to-function/
a+ib complex number1
c+id complex number2
realpart+imaginarypart
struct complex
{
int real;
int imaginary;
}c1,c2,c3;
c3.real=c1.real+c2.real;
c3.imaginary=c1.imaginary+c2.imaginary;
printf(“addition of two complex numbers is %d+
%di”,c3.real,c3.imaginary);
multiplication
c3.real=c1.real*c2.real-c1.imaginary*c2*imaginary;
c3.imaginary=c1.real*c2.imaginary+c1.imaginary*c2.real
;