# B4J QuestionThe Bits are Byting me in the ass!

#### Kevin

##### Well-Known Member
I never did quite grasp the concept of bits. I am trying to decode date and time stamps that are stored as follows:

Date is specified in decimal 16-bit integer in the following formats:

Bit 15-9
Year: Specify a value based on 0 as a 1980.

Bit 8-5

Month: Specify a value from 1 to 12.

Bit 4-0
Day: Specify a value from 1 to 31.

Time is specified in decimal 16-bit integer in the following formats:

Bit 15-11
Hour

Bit 10-5
Minute

Bit 4-0
Second / 2

So how can I work this out? This is all I have (actual sample values), which arguably is really nothing.

B4X:
``````Dim iDateStamp as Int = 18026
Dim iTimeStamp as Int = 24577

??????????``````

#### thedesolatesoul

##### Expert
B4X:
``````Dim Seconds as Int = Bit.And(iTimeStamp, 0x1F)
Dim Minutes  as Int =  Bit.ShiftRight(Bit.And(iTimeStamp, 0x3FF), 5)
Dim Hours    as Int =  Bit.ShiftRight(Bit.And(iTimeStamp, 0xFFFF), 11)``````

#### Kevin

##### Well-Known Member
I have no idea what the hex parts are doing, but I will give it a shot. Thanks!!!

#### thedesolatesoul

##### Expert
I haven't really tested it, just wanted to give you the concept.
The AND with hex which is really just bit positions is masking out unneeded bits. So if you want the first 5 bits you AND with binary 11111 which is 0x1F. So any number left over will be in the range if 0 to 31.

#### Kevin

##### Well-Known Member
Aha! Thanks for the further explanation. With that I managed to get the month part right!

B4X:
``Dim Month as Int = Bit.ShiftRight(Bit.And(iDateStamp, 0xFF), 5)``

I'll likely forget this by tomorrow but at least I can refer myself back to this thread. Thanks again!

Replies
6
Views
4K
Replies
4
Views
317
Replies
3
Views
191
Replies
8
Views
3K
Replies
7
Views
2K