/* File: dos-date.c Purpose: Illustrates low-level bit manipulation. Converts dates, as used in the Microsoft 16-bit format (e.g., Microsoft FAT) Author: Bill Slough Notes: See King, Ch. 20 for more details */ #include #define EPOCH 1980 /* beginning of time according to Microsoft */ /* * +--------------+----------+-----------+ * | year | month | day | * +--------------+----------+-----------+ * 7 bits 4 bits 5 bits */ struct filedate_t { unsigned int day: 5; unsigned int month: 4; unsigned int year: 7; }; union int_date { unsigned short i; struct filedate_t fd; }; void display_packed(int m, int d, int y); void display_mdy(unsigned short packed_date); int main(void) { int m, d, y; unsigned short ms_date; printf("Enter date (mm/dd/yyyy): "); scanf("%d/%d/%d", &m, &d, &y); display_packed(m, d, y); printf("Enter date (4-digit packed hexadecimal): "); scanf("%hx", &ms_date); display_mdy(ms_date); return 0; } /* Given a month, day, and year, show the MS-DOS packed form */ void display_packed(int m, int d, int y) { struct filedate_t mydate; union int_date u; mydate.month = m; mydate.day = d; mydate.year = y - EPOCH; u.fd = mydate; /* Display the date in its 16 bit, 3-field form */ printf("DOS date as 16 bit quantity: %4x\n\n", u.i); } /* Given the MS-DOS packed form, show the month, day and year */ void display_mdy(unsigned short packed_date) { union int_date u; u.i = packed_date; /* Display each field of the date */ printf("Month: %2d Day: %2d Year: %4d\n", u.fd.month, u.fd.day, u.fd.year + EPOCH); }