Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

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

Maybe this will clarify things a little.

This constructs a union between a unsigned 32-bit integer and a struct containing 32 x 1-bit fields.

It assigned the value 0x01234567 to the uint and then prints (from C) a string of 0s & 1s to reflect the bitfields, first to last in the struct.

You'll notice from the output (after __END__), that the first bitfield in the struct maps to the lsb in the integer; and the last to the msb; indicating that this is a little-endian (intel) machine.

It then passes the uint back to perl, packs it using the little-endian template 'V' and unpacks its bits using both 'b' and 'B'.

Note that the 'b' template mirrors the ordering of the bits as seen via the bitfields.

However, once you go beyond that into the realms of C++ coercions and bitsets, I'm afraid your on your own.

#! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1,; use Inline C => <<'END_C', NAME => 'bitfields', CLEAN_AFTER_BUILD =>0 +; #include "mytypes.h" union { struct { unsigned b00:1; unsigned b01:1; unsigned b02:1; unsigned b03:1 +; unsigned b04:1; unsigned b05:1; unsigned b06:1; unsigned b07:1 +; unsigned b08:1; unsigned b09:1; unsigned b10:1; unsigned b11:1 +; unsigned b12:1; unsigned b13:1; unsigned b14:1; unsigned b15:1 +; unsigned b16:1; unsigned b17:1; unsigned b18:1; unsigned b19:1 +; unsigned b20:1; unsigned b21:1; unsigned b22:1; unsigned b23:1 +; unsigned b24:1; unsigned b25:1; unsigned b26:1; unsigned b27:1 +; unsigned b28:1; unsigned b29:1; unsigned b30:1; unsigned b31:1 +; } bits; U32 uint; } X; U32 test( SV *unused ) { X.uint = 0x01234567; printf( "%u\n", X.uint ); printf( "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c% +c%c%c%c\n", X.bits.b00 ? '1' : '0', X.bits.b01 ? '1' : '0', X.bits.b02 ? '1' + : '0', X.bits.b03 ? '1' : '0', X.bits.b04 ? '1' : '0', X.bits.b05 ? '1' + : '0', X.bits.b06 ? '1' : '0', X.bits.b07 ? '1' : '0', X.bits.b08 ? '1' + : '0', X.bits.b09 ? '1' : '0', X.bits.b10 ? '1' : '0', X.bits.b11 ? '1' + : '0', X.bits.b12 ? '1' : '0', X.bits.b13 ? '1' : '0', X.bits.b14 ? '1' + : '0', X.bits.b15 ? '1' : '0', X.bits.b16 ? '1' : '0', X.bits.b17 ? '1' + : '0', X.bits.b18 ? '1' : '0', X.bits.b19 ? '1' : '0', X.bits.b20 ? '1' + : '0', X.bits.b21 ? '1' : '0', X.bits.b22 ? '1' : '0', X.bits.b23 ? '1' + : '0', X.bits.b24 ? '1' : '0', X.bits.b25 ? '1' : '0', X.bits.b26 ? '1' + : '0', X.bits.b27 ? '1' : '0', X.bits.b28 ? '1' : '0', X.bits.b29 ? '1' + : '0', X.bits.b30 ? '1' : '0', X.bits.b31 ? '1' : '0' ); return X.uint; } END_C my $uint = test( 1 ); print unpack 'b*', pack 'V', $uint; print unpack 'B*', pack 'V', $uint; __END__ C:\test>bitFields.pl 19088743 11100110101000101100010010000000 11100110101000101100010010000000 01100111010001010010001100000001

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 Re^3: Bit order in bytes by BrowserUk
in thread Bit order in bytes by geoffleach

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: (3)
As of 2024-04-19 20:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found