in reply to An array of boolean values in a bitfield

did you consider using a lookup-table to calculate the number of bits, changing time with memory-complexity?

for example

# for each character, count the on bits in that character my %bits_in; foreach my $char ( map { chr } 0 .. 255 ) { foreach my $bit ( 0 .. 7 ) { $bits_in{ $char } += vec( $char, $bit, 1 ); } }
could be written
# for each character, count the on bits in that character my %bits_in; foreach my $char ( map { chr } 0 .. 255 ) { $bits_in{ $char } = $sum[ $char ]; }

Cheers Rolf

Replies are listed 'Best First'.
Re^2: An array of boolean values in a bitfield
by kyle (Abbot) on Dec 03, 2008 at 18:08 UTC

    Where do you get the array @sum? The point of the loop that you're commenting on is specifically to build a lookup table that maps a chr to the number of '1' bits that are in it. That table gets used in some of the bit counting methods that follow.

      I get it, so @sum and %bits_in have the same purpose!

      I'm just confused, why do you care about the speed to calculate a constant table with only 256 entries? And why do you use a hash if speed counts? ... arrays are much faster!

      Cheers Rolf

        I'm not trying to optimize the creation of the lookup table. The lookup table is used to (more quickly) count bits in the bit field. I don't use an array for that because then the lookups would have to call ord to get the index into the array.