B4J Question The Bits are Byting me in the ass!

Kevin

Well-Known Member
Licensed User
Longtime User
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

??????????
 

Kevin

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

thedesolatesoul

Expert
Licensed User
Longtime User
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.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
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! :D
 
Upvote 0
Top