Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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:

#! perl -slw use strict; my $n = 0x80061861; my $top14 = ( $n & 0xfffc0000 ) >> 18; my $nxt6 = ( $n & 0x0003f000 ) >> 12; my $mid6 = ( $n & 0x00000fc0 ) >> 6; my $bot6 = ( $n & 0x0000003f ); print "$n => $top14 : $nxt6 : $mid6 : $bot6"; __END__ C:\test>bit 2147883105 => 8193 : 33 : 33 : 33

But I need to do this millions of times, so I'm looking to see if there is a more efficient way? (I don't care if I get 4 separate variables or an array with 4 values.)

Note: This is for me; no one else. Elegance, readability and maintainability are simply not considerations here. Just speed.

Any suggestions?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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 Efficient bit-twiddling in Perl. by BrowserUk

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-24 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found