Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Extracting Bit Fields (Again)

by BrowserUk (Patriarch)
on Dec 22, 2011 at 08:35 UTC ( [id://944736]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Extracting Bit Fields (Again)
in thread Extracting Bit Fields (Again)

Maybe something like this would work for you? (Update: simplified code):

Update 2: Unsimplified code to correct errors noted by oyster2011 below (Thanks!):

#! perl -slw use strict; sub bitFields{ my( $val, $pat ) = @_; my @fields = unpack $pat, reverse unpack 'b32', pack 'L', $val; return unpack 'L*', pack '(b32)*', map scalar( reverse), @fields; } my( $linkStatus, $cardStatus, $reserved, $intrStatus ) = bitFields( 123456789, 'x2 a2 a3 a7 a15' ); print for $linkStatus, $cardStatus, $reserved, $intrStatus; __END__ C:\test>bitFields.pl 0 3 86 31138

Use 'xn' in the template to skip over bits you aren't interested in. You might need to use 'B32' instead of 'b32' depending upon your platform.

Masking and shifting is almost certainly quicker, but this could be seen as a nice abstraction.

You could also do away with the intermediates if that floats your boat:

sub bitFields{ unpack 'L*', pack'(b32)*', map scalar( reverse), unpack $_[1], reverse unpack 'b32', pack 'L', $_[0]; }

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.

The start of some sanity?

Replies are listed 'Best First'.
Re^4: Extracting Bit Fields (Again)
by oyster2011 (Initiate) on Jan 02, 2012 at 06:22 UTC

    Thank you so much for the quick response. I was trying to understand the code output.

    But here is what I find: Input is : 123456789 (decimal) = 0x075BCD15 (hex, little endian) = 0000_0111_0101_1011_1100_1101_0001_0101 (binary)

    So, if we use the template 'x2 a2 a3 a7 a15', we should have 00 for x2 00 for a2 011 for a3 1010110 for a7 111100110100010 for a15 The output in hex should be 0 0 3 56 79A2 Is this right? I seem to be missing something here.

      we should have 00 for x2

      No. I did mention "Use 'xn' in the template to skip over bits you aren't interested in. ". See also pack.

      The output in hex should be 0 0 3 56 79A2

      So skipping the first field, and converting your expectations to decimal, the output should be:

      0 3 86 31138

      Which it wasn't, but will be now I've updated the code above!

      Thank you for bringing my mistake to my attention. :)


      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.

      The start of some sanity?

        Great! Works fine for me now. Thanks a lot, appreciate it!

        I am writing a template file that will have the bit field descriptions. This file will be parsed by the perl program eventually to generate human readable values for the register values

        Your function will be the core of this processing Thanks again!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://944736]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-25 19:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found