Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: In-place bitwise NOT?

by Grimy (Pilgrim)
on Jul 27, 2013 at 16:56 UTC ( #1046672=note: print w/ replies, xml ) Need Help??


in reply to In-place bitwise NOT?

Nobody proposed a vec solution yet. Here’s one:

use feature say; $_ = "012\xf0ABC\x0f\x00\x01\xff"; say unpack "H*"; # 303132f04142430f0001ff for (my $i = length; $i--; vec($_, $i, 8) ^= ~0) {} say unpack "H*"; # cfcecd0fbebdbcf0fffe00
Could probably be optimized for 32/64 bits. Whatever. I'm too lazy to benchmark it.


Comment on Re: In-place bitwise NOT?
Select or Download Code
Re^2: In-place bitwise NOT?
by BrowserUk (Pope) on Jul 27, 2013 at 17:18 UTC

    That was my first attempt :)

    sub byvec { no warnings 'portable'; vec( ${ $_[0] }, $_, 64 ) ^= ~0 for 0 .. ( length( ${ $_[0] } ) / +8 ); }

    Nowhere close to tr///.


    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (12)
As of 2015-07-06 15:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (77 votes), past polls