Function: logcount

logcount is a function defined in data.c.

Signature

(logcount VALUE)

Documentation

Return population count of VALUE.

This is the number of one bits in the two's complement representation of VALUE. If VALUE is negative, return the number of zero bits in the representation.

Other relevant functions are documented in the number group.

View in manual

Probably introduced at or before Emacs version 27.1.

Shortdoc

;; number
(logcount 5)
    => 2

Source Code

// Defined in /usr/src/emacs/src/data.c
{
  CHECK_INTEGER (value);

  if (BIGNUMP (value))
    {
      mpz_t const *nonneg = xbignum_val (value);
      if (mpz_sgn (*nonneg) < 0)
	{
	  mpz_com (mpz[0], *nonneg);
	  nonneg = &mpz[0];
	}
      return make_fixnum (mpz_popcount (*nonneg));
    }

  eassume (FIXNUMP (value));
  EMACS_INT v = XFIXNUM (value) < 0 ? -1 - XFIXNUM (value) : XFIXNUM (value);
  return make_fixnum (EMACS_UINT_WIDTH <= UINT_WIDTH
		      ? count_one_bits (v)
		      : EMACS_UINT_WIDTH <= ULONG_WIDTH
		      ? count_one_bits_l (v)
		      : count_one_bits_ll (v));
}