Path: news.mathworks.com!not-for-mail
From: "Andy Robb" <ajrobb@hotmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: count number of
Date: Sun, 3 Aug 2008 11:32:28 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 11
Message-ID: <g7450c$q2p$1@fred.mathworks.com>
References: <g6q9kq$log$1@fred.mathworks.com> <3412110.1217443328021.JavaMail.jakarta@nitrogen.mathforum.org>
Reply-To: "Andy Robb" <ajrobb@hotmail.com>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1217763148 26713 172.30.248.38 (3 Aug 2008 11:32:28 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 3 Aug 2008 11:32:28 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1374918
Xref: news.mathworks.com comp.soft-sys.matlab:483338



I prefer the following (which I didn't spot):

int bitcount(unsigned n) {
  n = ((n >> 1) & 0x55555555) + (n & 0x55555555);
  n = ((n >> 2) & 0x33333333) + (n & 0x33333333);
  n = ((n >> 4) & 0x0f0f0f0f) + (n & 0x0f0f0f0f);
  n += n >> 8;
  n += n >> 16;
  return n & 0xff;
}