I would hold your data in 3 bytes as
- first byte = year
- second byte = month
- third byte = day
There's plenty of room in each byte for the domain of each field (year, month, day). You could write them in an integer with bit-shifting operations, like this:
int year = 13;
int month = 7;
int day = 26;
int my_date = (year << 16) | (month << 8) | day;
Edit:
What I did in my_date: I basically concatenated the information you need (year, month, day) into a series of bits (8 bits per information field), as an integer. You know an int
is 4 bytes. Consider for starters that my_date
is 0, that is, all 32 bits are 0. The 4 bytes of it are as follows ("|" denotes concatenation; that's for ease of reading):
my_date = 0 | 0 | 0 | 0
When I write year << 16
I have
year << 16 = 0 | year | 0 | 0
In a similar fashion,
month << 8 = 0 | 0 | month | 0
day = 0 | 0 | 0 | day
When I apply the OR operator on all of them, my_date looks like this:
my_date = 0 | year | month | day
Accessing them:
year = (my_date & 0xFF0000) >> 16;
month = (my_date & 0xFF00) >> 8;
day = my_date & 0xFF;
Edit: how accessing works. We previously had
my_date = 0 | year | month | day
If you do, for example, an AND with 0xFF00, which is 0 | 0 | FF | 0
, you get
my_date & 0xFF00 = 0 | 0 | month | 0
Now all you need to do is shift your data back, so
(my_date & 0xFF00) >> 8 = 0 | 0 | 0 | month = month
Hope it's clearer now.