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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|