B4J Question The Bits are Byting me in the ass!

Discussion in 'B4J Questions' started by Kevin, Mar 11, 2015.

  1. Kevin

    Kevin Well-Known Member Licensed User

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




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

    Code:
    Dim iDateStamp as Int = 18026
    Dim iTimeStamp as Int = 24577

    ??????????
     
  2. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Code:
    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)
    Mask, Shift, Mask, Shift
     
    Kevin likes this.
  3. Kevin

    Kevin Well-Known Member Licensed User

    I have no idea what the hex parts are doing, but I will give it a shot. Thanks!!!
     
  4. thedesolatesoul

    thedesolatesoul Expert Licensed 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.
     
    Kevin likes this.
  5. Kevin

    Kevin Well-Known Member Licensed User

    Aha! Thanks for the further explanation. With that I managed to get the month part right!

    Code:
    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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice