Example 100: Sort Dates Using Bit Fields

Learn how to use bit fields in a struct.

Problem

Write a program that stores information about a date in a structure containing three members: day, month, and year.

Using bit fields, the day number should get stored in the first 5 bits, the month number in 4 bits, and the year in 12 bits.

Write a function to read the joining date of 5 employees and display them in ascending order of the year.

Example

Input Output
1 1 2010
2 2 2013
3 3 2011
4 4 2014
5 5 2013
1 1 2010
3 3 2011
2 2 2013
5 5 2013
4 4 2014

Try it yourself

Try to solve this question on your own in the code widget below. If you get stuck, you can always refer to the solution provided.

Note: Do not change the print loop given in the exercise.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.