Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Displaying bit-vectors with Most Significant Bits to the left?

by ikegami (Patriarch)
on Jan 05, 2020 at 14:21 UTC ( [id://11110978]=note: print w/replies, xml ) Need Help??


in reply to Re: Displaying bit-vectors with Most Significant Bits to the left?
in thread Displaying bit-vectors with Most Significant Bits to the left?

The OP didn't give any indication that $m0 was different than expected. Changing the value of $m0 doesn't seem appropriate. But if it is, there are two improvements you can make.


When doing bit arithmetic, most people prefer to use actual bit arithmetic instead of powers. Not only is it more intuitive to use operations that match one's mental model, your approach fails if a bit is already set. Replace

$val += 2 ** $_

with

$val |= 1 << $_

As evidenced by these posts, vec is quite weird, so it's surely best to avoid it if it doesn't offer a benefit.

my $vec; vec( $vec, 0, 16 ) = $val;

can be replaced with

my $vec = pack 'n', $val;

Replies are listed 'Best First'.
Re^3: Displaying bit-vectors with Most Significant Bits to the left?
by LanX (Saint) on Jan 05, 2020 at 15:18 UTC

    > vec is quite weird, so it's surely best to avoid it if it doesn't offer a benefit.

    I think it's trying to hide the machine representation of a number.

    I "grew up" on motorola, that's why I'm still confused.

    But vec's approach might provide reproducible results on different architectures.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      But vec's approach might provide reproducible results on different architectures.

      Each of the snippets I posted produces the same output on both little-endian and big-endian machines.

      With pack, you say what you want, and you get it. It's far cleaner than being told what you're going to get —sometimes LE, sometimes BE, based on the size of the third arg— and having to work around that.

        > Each of the snippets I posted produces the same output on both little-endian and big-endian machines

        I believe you, but how do you reliably check the output?

        And what does "same output" exactly mean?

        I did machine programming on big-endian architectures, I don't know what to trust anymore.

        Do I need to check with Devel::Peek to be sure?

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-25 13:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found