Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!ukc!icdoc!achilles!md From: md@doc.ic.ac.uk (Mark Dawson) Newsgroups: comp.lang.c Subject: Re: count of bits set in a long Keywords: bits set, count of, long Message-ID: Date: 25 Sep 90 11:07:51 GMT References: <37545@ut-emx.uucp> <3820@goanna.cs.rmit.oz.au> Sender: news@doc.ic.ac.uk Lines: 14 In <3820@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes: >There's a rather nice loop that does one iteration per bit, and works >for any size ... Here it is: num_set_bits = 0; while (word != 0) { /* there must be at least one bit set */ num_set_bits++; word &= (word - 1); /* remove the least significant set bit */ /* iterate... */ } Mark