Path: utzoo!attcan!uunet!crdgw1!antarctica!davidsen From: davidsen@antarctica.crd.GE.COM (william E Davidsen) Newsgroups: comp.lang.c Subject: Re: count of bits set in a long Keywords: bits set, count of, long Message-ID: <12330@crdgw1.crd.ge.com> Date: 2 Oct 90 14:00:57 GMT References: <37545@ut-emx.uucp> <1990Sep26.180240.11516@everexn.com> Sender: news@crdgw1.crd.ge.com Reply-To: davidsen@crdos1.crd.ge.com (bill davidsen) Organization: GE Corporate R&D Center, Schenectady NY Lines: 22 There have been lots of fast but non-portable solutions posted. I think this problem lends itself to a non-portable solution for speed, but please remember that if you assume (a) all longs have 32 bits or (b) all computers have 2's complement arithmetic, or (c) all bytes are 8 bits... ** All the world's not a VAX (or Sun) ** and many of the solutions posted are not going to work. I like the one which said (paraphrase) while (n) { count += (n & 1); n >>= 1; } -- Bill Davidsen (davidsen@crdos1.crd.ge.com, uunet!crdgw1!crdos1!davidsen) GE Corp R&D Center, Schenectady NY Moderator of comp.binaries.ibm.pc and 386users mailing list "This is your PC. This is your PC on OS/2. Any questions?"