Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: How to do popcount (aka Hamming weight) in Perl

by danaj (Friar)
on Sep 26, 2017 at 17:04 UTC ( [id://1200134]=note: print w/replies, xml ) Need Help??


in reply to Re: How to do popcount (aka Hamming weight) in Perl
in thread How to do popcount (aka Hamming weight) in Perl (popcount References)

Dana replaced popcnt with mpu_popcount_string in Math::Prime::Util v0.62.

I moved the native popcnt to util.h. mpu_popcount_string is a new function to handle bigints or sufficiently magic input without using a bigint library. It's faster than getting Math::BigInt involved until the numbers get over 500 digits. It should be optimized, but it's not exactly the common case.

It's also useful for making sure we don't get really slow for 64-bit numbers on a 32-bit Perl. Not as fast as forcing 64-bit code, but that's further narrowing down the space -- people on 64-bit machines who install a 32-bit Perl.

Using Math::BigInt to handle a long digit string would be something like:

use Math::BigInt; my $n = "16" x 1000; say 0 + (Math::BigInt->new("$n")->as_bin() =~ tr/1//);
where only the last part is needed if it's already a bigint. Math::GMPz has Rmpz_popcount which is ridiculously fast if the input is already a Math::GMPz object.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-26 05:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found