Practical / Solution
Bit-fields — Exercise 1
Problem
Write a C program to create a structure using bit-fields to store
a person's gender and age using limited number of bits.
Program
#include <stdio.h>
struct Person
{
unsigned int age : 7;
unsigned int gender : 1;
};
int main()
{
struct Person p;
p.age = 21;
p.gender = 1;
printf("Age = %u\n", p.age);
printf("Gender = %u\n", p.gender);
return 0;
}
Explanation
A bit-field specifies the number of bits used by a structure member.
Here, age uses 7 bits and gender uses 1 bit.
Expected Output
Bit-fields — Exercise 2
Problem
Write a C program to create bit-fields for storing day, month and
year components of a date.
Program
#include <stdio.h>
struct Date
{
unsigned int day : 5;
unsigned int month : 4;
unsigned int year : 12;
};
int main()
{
struct Date d;
d.day = 15;
d.month = 8;
d.year = 2026;
printf("Date = %02u/%02u/%u\n",
d.day,
d.month,
d.year);
return 0;
}
Explanation
Bit-fields can be used when values have known ranges. A day needs
only enough bits for values from 1 to 31, while a month needs enough
bits for values from 1 to 12.
Expected Output
Bit-fields — Exercise 3
Problem
Write a C program to use bit-fields for storing the status of three
devices using one-bit flags.
Program
#include <stdio.h>
struct DeviceStatus
{
unsigned int wifi : 1;
unsigned int bluetooth : 1;
unsigned int gps : 1;
};
int main()
{
struct DeviceStatus status;
status.wifi = 1;
status.bluetooth = 0;
status.gps = 1;
printf("WiFi = %u\n", status.wifi);
printf("Bluetooth = %u\n", status.bluetooth);
printf("GPS = %u\n", status.gps);
return 0;
}
Explanation
A one-bit field is suitable for values that have only two states,
such as ON/OFF or enabled/disabled. Each member can store 0 or 1.
Expected Output
WiFi = 1
Bluetooth = 0
GPS = 1
Bit-fields — Exercise 4
Problem
Write a C program to store a student's class year and section using
bit-fields and display the values.
Program
#include <stdio.h>
struct Student
{
unsigned int year : 2;
unsigned int section : 2;
};
int main()
{
struct Student s;
s.year = 2;
s.section = 1;
printf("Year = %u\n", s.year);
printf("Section = %u\n", s.section);
return 0;
}
Explanation
The width after the colon specifies how many bits are allocated.
A 2-bit unsigned field can represent values from 0 to 3.
Expected Output
Bit-fields — Exercise 5
Problem
Write a C program to take user input for three ON/OFF device flags
stored using bit-fields.
Program
#include <stdio.h>
struct Device
{
unsigned int fan : 1;
unsigned int light : 1;
unsigned int ac : 1;
};
int main()
{
struct Device d;
printf("Enter fan status (0/1): ");
scanf("%u", &d.fan);
printf("Enter light status (0/1): ");
scanf("%u", &d.light);
printf("Enter AC status (0/1): ");
scanf("%u", &d.ac);
printf("\nFan = %u\n", d.fan);
printf("Light = %u\n", d.light);
printf("AC = %u\n", d.ac);
return 0;
}
Explanation
Each device status requires only one bit because the value is either
0 or 1. Bit-fields allow these small values to be represented compactly.
Expected Output
Enter fan status (0/1): 1
Enter light status (0/1): 0
Enter AC status (0/1): 1
Fan = 1
Light = 0
AC = 1
Bit-fields — Exercise 6
Problem
Write a C program to demonstrate a bit-field used for storing a
small numeric value such as a priority level from 0 to 7.
Program
#include <stdio.h>
struct Task
{
unsigned int priority : 3;
};
int main()
{
struct Task t;
t.priority = 5;
printf("Task Priority = %u\n",
t.priority);
return 0;
}
Explanation
A 3-bit unsigned field can represent values from 0 through 7.
Therefore, it is appropriate for a small priority value in this example.
Expected Output
Bit-fields — Exercise 7
Problem
Write a C program to create a user account status using bit-fields
for active, verified and administrator flags.
Program
#include <stdio.h>
struct Account
{
unsigned int active : 1;
unsigned int verified : 1;
unsigned int administrator : 1;
};
int main()
{
struct Account user;
user.active = 1;
user.verified = 1;
user.administrator = 0;
printf("Active = %u\n", user.active);
printf("Verified = %u\n", user.verified);
printf("Administrator = %u\n",
user.administrator);
return 0;
}
Explanation
Bit-fields are useful for storing multiple Boolean flags. Each flag
can occupy one bit and represent a true or false state.
Expected Output
Active = 1
Verified = 1
Administrator = 0
Bit-fields — Exercise 8
Problem
Write a C program to modify individual status flags in a structure
using bit-fields.
Program
#include <stdio.h>
struct Status
{
unsigned int power : 1;
unsigned int online : 1;
unsigned int error : 1;
};
int main()
{
struct Status s;
s.power = 1;
s.online = 0;
s.error = 0;
printf("Initial Status\n");
printf("Power = %u\n", s.power);
printf("Online = %u\n", s.online);
printf("Error = %u\n", s.error);
/* Change individual flags */
s.online = 1;
s.error = 1;
printf("\nUpdated Status\n");
printf("Power = %u\n", s.power);
printf("Online = %u\n", s.online);
printf("Error = %u\n", s.error);
return 0;
}
Explanation
Individual bit-field members can be assigned new values just like
ordinary structure members. Only the specified field is modified.
Expected Output
Initial Status
Power = 1
Online = 0
Error = 0
Updated Status
Power = 1
Online = 1
Error = 1
Bit-fields — Exercise 9
Problem
Write a C program to store a student's grade category and pass status
using bit-fields.
Program
#include <stdio.h>
struct Result
{
unsigned int grade : 3;
unsigned int pass : 1;
};
int main()
{
struct Result r;
r.grade = 5;
r.pass = 1;
printf("Grade Code = %u\n", r.grade);
printf("Pass Status = %u\n", r.pass);
return 0;
}
Explanation
The grade field uses 3 bits for a small numeric grade code,
while pass uses one bit because it has only two possible
states.
Expected Output
Grade Code = 5
Pass Status = 1
Bit-fields — Exercise 10
Problem
Write a C program to create a practical system-status record using
multiple bit-fields and display all flags.
Program
#include <stdio.h>
struct SystemStatus
{
unsigned int powerOn : 1;
unsigned int network : 1;
unsigned int batteryLow : 1;
unsigned int error : 1;
unsigned int mode : 2;
};
int main()
{
struct SystemStatus system;
system.powerOn = 1;
system.network = 1;
system.batteryLow = 0;
system.error = 0;
system.mode = 2;
printf("============================\n");
printf(" SYSTEM STATUS\n");
printf("============================\n");
printf("Power On : %u\n",
system.powerOn);
printf("Network : %u\n",
system.network);
printf("Battery Low : %u\n",
system.batteryLow);
printf("Error : %u\n",
system.error);
printf("Mode : %u\n",
system.mode);
return 0;
}
Explanation
This example combines several bit-fields in a single structure. One-bit
fields represent Boolean status flags, while the 2-bit mode
field can represent a small range of numeric values.
Expected Output
============================
SYSTEM STATUS
============================
Power On : 1
Network : 1
Battery Low : 0
Error : 0
Mode : 2