Update: Simplified the packing by removing a redundant step.

Update2: eliminated the map from the packing.

Non-trivial:

#! perl -slw use strict; use Data::Dump qw[ pp ]; use Math::Random::MT qw[ rand ]; my @sint24s = map{ -2**23 + int( rand 2**24 ) } 1 .. 20; ## removed duplicated my $packed -- Thanks to AnonmalousMonk my $packed = join '', unpack '(a3x)*', pack 'l*', @sint24s; my @unpacked = map { unpack 'l', $_ . chr( vec( $_, 23, 1 ) ? 255 : 0 ); } unpack '(a3)*', $packed; print "$sint24s[ $_ ] ;; $unpacked[ $_ ]" for 0 .. $#sint24s; __END__ C:\test>sint24 4243386 ;; 4243386 4809369 ;; 4809369 -888567 ;; -888567 -7576685 ;; -7576685 1987080 ;; 1987080 -2170022 ;; -2170022 -1135866 ;; -1135866 1924446 ;; 1924446 6348263 ;; 6348263 1911716 ;; 1911716 -1791354 ;; -1791354 -8343943 ;; -8343943 -6224088 ;; -6224088 3919567 ;; 3919567 -1176382 ;; -1176382 6288012 ;; 6288012 -5569609 ;; -5569609 -5363232 ;; -5363232 -1344267 ;; -1344267 3649155 ;; 3649155

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.

In reply to Re: 3-byte representation (Simplified!) by BrowserUk
in thread 3-byte representation by gerleu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.