in reply to Re: Practical Example of Converting Packed Value
in thread Practical Example of Converting Packed Value

Thank you, ++BrowserUk!

I actually didn't ask for solutions using pack/unpack; I only described the representation format as "packed". Please feel free to show me how you would do it using bitwise math.

Jim

  • Comment on Re^2: Practical Example of Converting Packed Value

Replies are listed 'Best First'.
Re^3: Practical Example of Converting Packed Value
by ikegami (Patriarch) on Dec 01, 2008 at 00:50 UTC

    Please feel free to show me how you would do it using bitwise math.

    That's what the second snippet does. First, unpack 's' is used to convert the bytes representing a number into something Perl understands to be a number. Then bit math is performed to extract each of the fields.

      Yeah, BrowserUk was adding that last bit while I was too hastily replying to his very helpful post.

      I understand the bitmask and the right-shift operations much better now. This example had the concreteness and immediacy I needed. It helps me a lot to display the bitmask AND operation in binary.

      my $day = ( $short_int & 0b0000000_0000_11111 ) ; my $month = ( $short_int & 0b0000000_1111_00000 ) >> 5 ; my $year = ( $short_int & 0b1111111_0000_00000 ) >> 9 ; my $date = sprintf "%04d-%02d-%02d", 1980 + $year, $month, $day;
      Is there any reason not to use these binary constants instead of hexadecimal constants in the real program?

      I suppose in a real program, the binary/hexadecimal constants and the year 1980 would be symbolic constants; e.g., DAY_MASK and EPOCH_YEAR. Or not.

        Is there any reason not to use these binary constants instead of hexadecimal constants in the real program?

        Absolutely not! I actually had to write the binary down and convert to derive the hex.

        Why did I do that? Simply, because I forgot that Perl can handle binary constants. Dumb, but true.

        I especially like your use of the benign underscore to clarify the breakdown++


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        Is there any reason not to use these binary constants instead of hexadecimal constants in the real program?

        One advantage of using hex/oct is that you can avoid having long, unreadable strings of bits in your code. The shortness of the masks and your use of "_" invalidates negates that advantage. It actually makes bin more readable.

        Another advantage of using hex/oct for masks is that it allows you to interpret data in hex/oct dumps more easily.

        Balance your need for readable raw data with your need for code readability and chose the one that suits you best. It's purely a judgment call.

        I suppose in a real program, the binary/hexadecimal constants and the year 1980 would be symbolic constants

        They're not going to change and their purpose is quite obvious based on the variable names. I don't think creating the seven symbolic constants would improve the code unless they are used in multiple locations.