class String

Public Instance Methods

popcount() click to toggle source

Return the number of 1 bits in all the bytes of this ‘String`. @example

"abc".popcount # => 10

@return [Integer]

static VALUE
str_popcount(VALUE str)
{
  uchar *p    = (uchar*)RSTRING_PTR(str);
  long length = RSTRING_LEN(str);
  long bits   = 0;

  /* This could be made faster by processing 4/8 bytes at a time */

  while (length--)
    bits += __builtin_popcount(*p++);

  return LONG2FIX(bits);
}