public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Number of 1's in 64 bit number...[don't read if not interested in algos/math...]
@ 2002-04-24 21:55 Preeti Aira
  2002-04-24 22:35 ` Alan Modra
  2002-04-24 23:08 ` Vivek Srivastava
  0 siblings, 2 replies; 14+ messages in thread
From: Preeti Aira @ 2002-04-24 21:55 UTC (permalink / raw)
  To: gcc-help, gcc

Hello every body..

Can any body give me the algo for finding number of 1's  (set bits) in a 64 
bit number. Algo with out any loop or recurson.  Should be a reasonably 
efficient and small and giving result in fixed time.

have a nice time
Pret.

_________________________________________________________________
Join the worldÂ’s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com

^ permalink raw reply	[flat|nested] 14+ messages in thread
* Re: Number of 1's in 64 bit number...[don't read if not interested in algos/math...]
@ 2002-04-25  9:46 kelley.r.cook
  2002-04-25 10:13 ` Wei Qin
  0 siblings, 1 reply; 14+ messages in thread
From: kelley.r.cook @ 2002-04-25  9:46 UTC (permalink / raw)
  To: Preeti Aira, gcc, gcc-help

int count (long long x) {

int result;

result =
(x & 0x00000001 ? 1 : 0) +
(x & 0x00000002 ? 1 : 0) +
(x & 0x00000004 ? 1 : 0) +
(x & 0x00000008 ? 1 : 0) +
(x & 0x00000010 ? 1 : 0) +
(x & 0x00000020 ? 1 : 0) +
(x & 0x00000040 ? 1 : 0) +
(x & 0x00000080 ? 1 : 0) +
(x & 0x00000100 ? 1 : 0) +
(x & 0x00000200 ? 1 : 0) +
(x & 0x00000400 ? 1 : 0) +
(x & 0x00000800 ? 1 : 0) +
(x & 0x00001000 ? 1 : 0) +
(x & 0x00002000 ? 1 : 0) +
(x & 0x00004000 ? 1 : 0) +
(x & 0x00008000 ? 1 : 0) +
(x & 0x00010000 ? 1 : 0) +
(x & 0x00020000 ? 1 : 0) +
(x & 0x00040000 ? 1 : 0) +
(x & 0x00080000 ? 1 : 0) +
(x & 0x00100000 ? 1 : 0) +
(x & 0x00200000 ? 1 : 0) +
(x & 0x00400000 ? 1 : 0) +
(x & 0x00800000 ? 1 : 0) +
(x & 0x01000000 ? 1 : 0) +
(x & 0x02000000 ? 1 : 0) +
(x & 0x04000000 ? 1 : 0) +
(x & 0x08000000 ? 1 : 0) +
(x & 0x10000000 ? 1 : 0) +
(x & 0x20000000 ? 1 : 0) +
(x & 0x40000000 ? 1 : 0) +
(x & 0x80000000 ? 1 : 0);
return result;
}

^ permalink raw reply	[flat|nested] 14+ messages in thread
* Re: Number of 1's in 64 bit number...[don't read if not interested in algos/math...]
@ 2002-04-27 12:44 David Rasmussen
  0 siblings, 0 replies; 14+ messages in thread
From: David Rasmussen @ 2002-04-27 12:44 UTC (permalink / raw)
  To: gcc

I'm amazed to read all these suggestions. The problem is old and 
wellknown. It is usually called Population Count or PopCount. It is used 
in several situations, but the use I know best is from bitboard-based 
chess programs. Do a search of "chess bitboard" in google, and read 
about. The lookup-table is the usual implementation if potability is 
required. There are usually faster methods on a given platform, and some 
CPU's have this as a native instruction.

In chess, the board is often relatively sparse and the following 
implementation is better overall than the lookup-table for such 
programs. It loops one time for each 1-bit:

int PopCount(unsigned long long bitboard)
{
         int count = 0;
         while(bitboard)
         {
                 ++count;
                 bitboard &= bitboard - 1;
         }
         return count;
}

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2002-04-27 18:58 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-24 21:55 Number of 1's in 64 bit number...[don't read if not interested in algos/math...] Preeti Aira
2002-04-24 22:35 ` Alan Modra
2002-04-24 23:08 ` Vivek Srivastava
2002-04-24 23:51   ` Jaswant
2002-04-25  0:39   ` Gerald Pfeifer
2002-04-25  9:46 kelley.r.cook
2002-04-25 10:13 ` Wei Qin
2002-04-25 10:36   ` Nathan Sidwell
2002-04-25 11:24     ` Gabriel Paubert
2002-04-25 12:15     ` Wei Qin
2002-04-25 15:15       ` Richard Henderson
2002-04-25 15:47         ` Wei Qin
2002-04-25 17:55   ` Daniel Berlin
2002-04-27 12:44 David Rasmussen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).