[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [Fun With Perl] Another oddity from the days of assembler



Bernie Cosell wrote:
> 
> this little snippet is not Perl-specific, but it is a bit tricky to
> figure out what is going on:
> 
> sub s
> {
>     my $n = $_[0] ;
>     my $x = 0 ;
>     while ($n) { $n &= $n-1, $x++ ; }
>     return $x ;
> }

Hmm, isn't that the same as

 sub s {
   my $b = unpack("B*", pack("L*", shift));
   $b =~ s/0+//g;
   return length($b);
 }

This is probably very inefficient, compared to the original
version, but as perl is intended to work mainly with strings...

Remember: There is more than one way to do it! ;-)

Roland
--
perl -e '@_=@ARGV;$_="@_[-3,-2,-1]";s{([@{[$_[-1]]}])}#($0=$1.
${[qw[>$%, ()%{&", &"=, |}+&" *]]}[$@++])=~y/*=->$%&"(-){-}+,/
lusternohack /,$0#eg; print; '        -- Roland Giersig, JAPH.

==== Want to unsubscribe from this list? (Don't you love us anymore?)
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org