Use bitwise operators. From the picture it follows that the time field has sixteen bits with the following layout:
hhhhhmmmmmmsssss
0111110100011100 = 0x7d1c
Now, you want to mask out the bits that you are interested in. Let's say we want to get the number of hours first. We use the bitwise AND-operator (&
) to mask out these bits. The AND-operator takes two operands. It will make each bit that is 1
in both operands also a 1
, and all others 0
.
hhhhhmmmmmmsssss
0111110100011100 = 0x7d1c
1111100000000000 = 0xf800
----------------
0111100000000000 = 0x7d1c & 0xf800 = 0x7800
Now you have only the bits of interest, the 'hour' bits. However, the zeroes to the right are not meaningful to us, so we use the right shift operator (>>
) to move the bits into the right place, 11 places to the right.
0111100000000000 = 0x7d1c & 0xf800 = 0x7800
0000000000001111 = (0x7d1c & 0xf800) >> 11 = 0xF = 15
This is your result; 15 hours. The general rule in this case is this:
hours = (value & 0xf800) >> 11;
minutes = (value & 0x7e0) >> 5;
seconds = (value & 0x1f) * 2; // the number of seconds is rounded to an even number
// to save a bit, so multiply by two (see picture).
I'll leave it to you to verify the last two lines. Note that the windows calculator on 'programmer mode' is an invaluable tool for binary / hex / decimal conversions.