4
\$\begingroup\$

This is a copy of a posting I made on SO, it was suggested I post it here all I've changed from that post is I'm now doing the bitwise AND using a decimal number rather than using bindec("111") to create the mask. The output is identical.

I have a 32 bit number represented as a HEX string that I need to convert to a timestamp following these rules:

Timestamp specification

What I want to check is that I'm not doing something glaringly stupid? Does my conversion look sane given the timestamp specification?

I've put together the following code to do this. I know PHP isn't the best for this sort of work but it's part of a larger legacy application all written in PHP.

$t is taken from an XML file of readings, one every 5 mins.

    // convert the hex string to a string representation of the binary number
    // sprintf pads leading zeros, for debugging only
    $binStr = sprintf('%032s', base_convert($t, 16, 2));

    //get a decimal representation so we can use the bitwise operator on it
    $decimal = base_convert($t, 16, 10);

     // get the first 6 bits
    $min = $decimal & 63; // 111111 in binary

    // drop the first 8 bits then apply a mask to the next 5
    $hour = ($decimal >> 8) & 31; // 11111 in binary

    // drop the first 15 bits then apply a mask to the next 1
    $daylightSaving = ($decimal >> 15) & 1; 

    // etc
    $day  = ($decimal >> 16) & 31; // 11111
    $yearLsb  = ($decimal >> 21) & 7; //111
    $month = ($decimal >> 24) & 15; //1111
    $yearMsb  = ($decimal >> 28) & 15; //1111

    Logger::debug(
        "Incoming timestamp " . $t . " \n" . 
        " (as binary " . $binStr . ") converted to: \n" . 
        " Day: " . $day . "\n" . 
        " Month: " . $month . "\n" . 
        " Year: " . $yearMsb . $yearLsb . "\n" . 
        " Hour: " . $hour . "\n" . 
        " Min: " . $min . "\n" . 
        " Daylight saving: ". $daylightSaving
    );

The output for the last entry in the file is:

Incoming timestamp 2DAC1B0A
 (as binary 00101101101011000001101100001010) converted to
 Day: 12
 Month: 13
 Year: 25
 Hour: 27
 Min: 10
 Daylight saving: 0

I know the file was created at 11:25am on the 12th Nov 2013, the readings are for 11:10 to 11:25 with the last one in the file being the 11:10am one.

The mins match those in the file I get 10 through 25 in 5 min increments. The day is also accurate if I run it against other files.

There's a chance that the equipment manufacturer who wants us to purchase some expensive software to do this parsing has munged the timetamp specification but that being an option I've played with lots of possible combinations in that 32bit integer and I can't see how I can get all the numbers I require from the supplied timestamps.

\$\endgroup\$
2
\$\begingroup\$

Not being a PHP expert, but messing with bitwise operations a lot, I have some advice for you:

First, the specification for your operations should be embedded with the code as a comment. Having to look up a specification that may or may not be the same as the one you used when programming it, is not useful... and changing between screens/pages to inspect the code vs. the specification is also not much fun. Something like:

// Input data is 4 bytes with following bit-specification:
// Byte0  (least significant)
//     0-5 : minutes
//     6   : reserved
//     7   : valid
// Byte1
//     0-4 : hours
//    ........

This makes it easier to compare/validate your code.

My next suggestion is to shift the data as you pull values off... This way you don't need to do 'brain-mushing' thinking when doing the manipulation.

// get the first 6 bits
$min = $decimal & 63; // 111111 in binary
$decimal = $decimal >> 6; // get rid of those 6 bits,
                          // now we only need to worry about low bits.

Then, the next suggestion is to set up some 'constants' for your masks:

$mask6bits = (1 << 6) - 1; // 00111111 in binary.

Now, the example above becomes:

// get the first 6 bits
$min = $decimal & mask6bits;
$decimal = $decimal >> 6; // shift off minutes

Then, I recommend you process the fields you don't need, and it helps you keep track of the specification:

// get the first 6 bits
$min = $decimal & mask6bits;
$decimal = $decimal >> 6; // shift off minutes

$decimal = $decimal >> 1; // shift off reserved

$decimal = $decimal >> 1; // shift off valid

$hours = $decimal & mask5bits;
$decimal = $decimal >> 5; // shift off the hours.

Follow the same pattern for the other bits...

This is a relatively concise way to do things, actually, and you only need to keep one 'dimension' (the length of each data value) of the problem in your head, and you can ignore the offset.

This leads to fewer bugs, and better understanding and readability.

I cannot find a definitive reference whether PHP supports a definitive >>= assignment operator. If it does, the lines $decimal = $decimal >> 5 can be replaced with $decimal >>= 5

\$\endgroup\$
  • \$\begingroup\$ @rolfi thanks, great response. I already have the comment at the start of my method like you suggest, documenting the integer's make-up, but your other points I'll definitely take on board and apply. I'm starting to suspect that the timestamp doesn't follow this rule and it's a proprietary make-up. \$\endgroup\$ – Darren Beale Dec 10 '13 at 17:10
  • \$\begingroup\$ I'm new to CR so after reading the About page for the site I'm marking this as an answer: I've been given assistance on improving my code both for my sake and any future maintainers. \$\endgroup\$ – Darren Beale Dec 10 '13 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.