<?xml version="1.0" encoding="windows-1252"?>
<node id="1021064" title="Efficient bit-twiddling in Perl." created="2013-02-28 09:47:32" updated="2013-02-28 09:47:32">
<type id="115">
perlquestion</type>
<author id="171588">
BrowserUk</author>
<data>
<field name="doctext">
&lt;blockquote&gt;&lt;i&gt;&lt;/i&gt;&lt;/blockquote&gt;

&lt;p&gt;I need to break unsigned 32-bit values into 4 parts: (from the msb) 14-bits, 6-bits, 6-bits, 6-bits; (using perl rather than I::C or XS). That can be done as follows:
&lt;code&gt;
#! perl -slw
use strict;

my $n = 0x80061861;
my $top14 = ( $n &amp; 0xfffc0000 ) &gt;&gt; 18;
my $nxt6  = ( $n &amp; 0x0003f000 ) &gt;&gt; 12;
my $mid6  = ( $n &amp; 0x00000fc0 ) &gt;&gt;  6;
my $bot6  = ( $n &amp; 0x0000003f );

print "$n =&gt; $top14 : $nxt6 : $mid6 : $bot6";
__END__
C:\test&gt;bit
2147883105 =&gt; 8193 : 33 : 33 : 33
&lt;/code&gt;

&lt;p&gt;But I need to do this millions of times, so I'm looking to see if there is a more &lt;i&gt;efficient&lt;/i&gt; way? (I don't care if I get 4 separate variables or an array with 4 values.)

&lt;p&gt;Note: This is for me; no one else. Elegance, readability and maintainability are simply not considerations here. Just speed.

&lt;p&gt;Any suggestions?


&lt;div class="pmsig"&gt;&lt;div class="pmsig-171588"&gt;
&lt;hr /&gt;
&lt;font size=1 &gt;
&lt;div&gt;With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'&lt;/div&gt;
&lt;div&gt;Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.&lt;/div&gt;
&lt;div&gt;"Science is about questioning the status quo. Questioning authority". &lt;/div&gt;
&lt;div&gt;In the absence of evidence, opinion is indistinguishable from prejudice.
&lt;/div&gt;
&lt;/font&gt;

&lt;/div&gt;&lt;/div&gt;</field>
</data>
</node>
