home *** CD-ROM | disk | FTP | other *** search
- /*
- Newsgroups: comp.sys.amiga.programmer
- Path: usenet.ee.pdx.edu!cs.uoregon.edu!sgiblab!sgigate.sgi.com!olivea!charnel!rat!usc!sol.ctr.columbia.edu!news.kei.com!yeshua.marcam.com!zip.eecs.umich.edu!umn.edu!kksys.com!haapi!bee!sar
- From: sar@bee.beehive.mn.org (Steven A. Reisman)
- Subject: Re: counting 1 bits
- References: <1993Dec13.142639.12004@cs.kuleuven.ac.be>
- Organization: Steven Reisman & Associates
- Date: Tue, 14 Dec 1993 04:29:00 GMT
- X-Newsreader: TIN [version 1.1 PL8]
- Message-ID: <1993Dec14.042900.10161@bee.beehive.mn.org>
- Lines: 29
-
- Stefaan Decorte (stefaan@cs.kuleuven.ac.be) wrote:
-
- : Does there exist an (efficient) instruction to count the number of 1 bits in a
- : unsigned long data type? (preferably in C but assembler is also ok).
- : Or does there exist a way around performing the inefficient 32-part loop?
-
- Try the following:
- */
-
- #include <stdio.h>
-
- int cardcount(unsigned long x)
- {
- x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
- x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
- x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
- return x % 255;
- }
-
- main ()
- {
- printf("%d\n", cardcount(0xFFFFFFFF));
- printf("%d\n", cardcount(0x12345678));
- printf("%d\n", cardcount(0x12481248));
- }
-
- /*
- --
- Steven A. Reisman
- 12695 4th St. S. sar@beehive.mn.org
- Afton, MN 55001 (612) 436-7125
- */
-