Path: utzoo!attcan!uunet!mcsun!hp4nl!sci.kun.nl!atcmpe!jc From: jc@atcmp.nl (Jan Christiaan van Winkel) Newsgroups: comp.lang.c Subject: Re: count of bits set in a long Message-ID: <661@atcmpe.atcmp.nl> Date: 25 Sep 90 12:04:35 GMT References: <37545@ut-emx.uucp> Organization: AT Computing, Nijmegen, The Netherlands Lines: 19 From article <37545@ut-emx.uucp>, by nwagner@ut-emx.uucp (Neal R. Wagner): > > I need a fast method to count the number of bits that are set in a 32-bit > integer on a Sun 3/80 running 4.0.3 SunOS. The brute-force method follows. How about: int bit_count(i) unsigned long i; { int k=0; for (; i!=0; i>>=1) k += (i & 1); return k; } -- ___ __ ____________________________________________________________________ |/ \ Jan Christiaan van Winkel Tel: +31 80 566880 jc@atcmp.nl | AT Computing P.O. Box 1428 6501 BK Nijmegen The Netherlands __/ \__/ ____________________________________________________________________