The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


String::BitCount - count number of "1" bits in strings


  use String::BitCount;

  # get the number of bits in $string.
  my $count = BitCount($string);

  # prints '334'; The number of bits in the codes
  # of 'a', 'b' and 'c' are '3', '3' and '4'.
  print showBitCount('abc'), "\n";


BitCount LIST

Returns the the total number of "1" bits in the list.

showBitCount LIST

Copies the elements of LIST to a new list and converts the new elements to strings of digits showing the number of set bits in the original byte. In array context returns the new list. In scalar context joins the elements of the new list into a single string and returns the string. Only code points in the range 0x00 .. 0xFF are allowed.


The original BitCount design predated the introducing of the 'b' pack template. Now you should use $bit_count = unpack("%32b*", $string).

The arguments of showBitCount are restricted to strings of characters having code points in the range 0x00 .. 0xFF. If any string argument has code points greater than 0xFF (255) a "Wide character" warning will be issued.


Winfried Koenig <>