I have the following assignment:
Considering that the 32 bits on an unsigned integer represent a date like this:
Bits 0 to 7 are the day
Bits 8 to 23 are the year
Bits 24 to 31 are the month
Implement the function unsigned int greater_date(unsigned int date1, unsigned int date2) that returns the greater of the two dates passed as parameters.
How can I extract the day, year and month using shifts of bits?
I developed this:
n = 16;
year1 = date1 << n; // shift left
year2 = date2 << n; // shift left
n = 16;
year1 = year1 >> n; // shift right
year2 = year2 >> n; // shift right
n = 24;
month1 = date1 >> n; // shift right
month2 = date2 >> n; // shift right
n=8;
day1 = date1 << n; // shift left
day2 = date2 << n; // shift left
n = 24;
day1 = day1 >> n; // shift right
day2 = day2 >> n; // shift right
But this fails in some of the tests my program has to run through, even tho i tested with a quick main and it show the correct date.