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

How to do popcount (aka Hamming weight) in Perl (popcount References)

by eyepopslikeamosquito (Archbishop)
on Sep 24, 2017 at 10:23 UTC ( [id://1199987]=perlquestion: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    use strict;
    use warnings;
    ...
       Two   => sub { popcount2($_) for $start .. $end },
       Three => sub { popcount3($_) for $start .. $end },
    };
    
  2. or download this
    sanity test for correctness
    Benchmark: timing 50 iterations of One, Three, Two...
       One: 29 wallclock secs (28.41 usr +  0.00 sys = 28.41 CPU) @  1.76/
    +s (n=50)
     Three: 10 wallclock secs (10.00 usr +  0.00 sys = 10.00 CPU) @  5.00/
    +s (n=50)
       Two: 10 wallclock secs (10.03 usr +  0.00 sys = 10.03 CPU) @  4.98/
    +s (n=50)
    
  3. or download this
    // Make GCC __builtin_popcountll available with MSVC
    // (used to calculate the Hamming weight efficiently)
    ...
    #define __builtin_popcountll __popcnt64
    #define __builtin_popcount   __popcnt
    #endif
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1199987]
Approved by haukex
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-25 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found