Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Turning very larger numbers into an array of bits

by Marshall (Canon)
on Feb 06, 2017 at 02:58 UTC ( [id://1181158]=note: print w/replies, xml ) Need Help??


in reply to Turning very larger numbers into an array of bits

Essentially if I start with the number 57, I want to end up with the array ( 1, 0, 0, 1, 1, 1 ) (low bit is the first element).

Alternatively, if I just had a good way of checking the xth bit of a number without building a bitmask (unless someone can clue me in on how to do that quickly) that would work.

This just sounds so bizarre to me that I can't comprehend what is being attempted. To reverse 57 decimal, 111001b to 100111b is extremely counter intuitive! Yes, there are assembly level instructions that can do that with multiple instructions involving various kinds of masks and shifts but, that would be a wild thing to do. I think this is even more difficult to do in Perl.

There is no faster way of testing if a bit is set other than using a bit mask and a logical "and" instruction. The lowest level assembly language instructions do it that way.

  • Comment on Re: Turning very larger numbers into an array of bits

Replies are listed 'Best First'.
Re^2: Turning very larger numbers into an array of bits
by johngg (Canon) on Feb 06, 2017 at 11:41 UTC
    there are assembly level instructions that can do that with multiple instructions involving various kinds of masks and shifts but, that would be a wild thing to do. I think this is even more difficult to do in Perl.
    johngg@shiraz:~/perl/Monks > perl -Mstrict -Mwarnings -E ' my @arr = split m{}, do { my $vec; vec( $vec, 0, 8 ) = 57; unpack q{b*}, $vec; }; say for @arr;' 1 0 0 1 1 1 0 0

    Not really. This is Perl after all :)

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1181158]
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: (4)
As of 2024-04-19 21:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found