...though, apparently Kernighan didn't invent the method. From the link I
posted:


Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan
and Dennis M. Ritchie) mentions this in exercise 2-9. On April 19, 2006 Don
Knuth pointed out to me that this method "was first published by Peter
Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick
Lehmer and published in 1964 in a book edited by Beckenbach.)"



On 5/24/07, Patrick Burrows <[EMAIL PROTECTED]> wrote:

Here's how Kernighan does it (the K in K&R)[1], translated to C#:

int x = 3;
int c;
for (c = 0; x!=0; c++)
{
    x &= x - 1;
}

the above has the advantage over your original code in that it only loops
for the number of bits that are set, instead of over every bit.


[1]
http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan


On 5/24/07, Sawan Sharma <[EMAIL PROTECTED]> wrote:
>
> Hi All,
>
> I am using Solr facets logic to implement faceted searching in my web
> site.
>
> One problem I am facing here is that I am using BitArray class of .NET
> instead of BitSet class of java. The BitSet class has one method
> "Cardinality" which is not available in BitArray. This method returns
> number
> of True bits in BitSet. To achieve this goal I have used following
> logic...
>
>       Dim c As Integer = 0
>        For Each bit As Boolean In BitArray
>            If bit Then
>                c += 1
>            End If
>        Next
>        Return c
>
> Here I have used Loop and it consume little bit more time while
> preparing
> facets.
>
> I would be interested in hearing what others think about this problem
> and
> how to best implement this functionality.
>
> Thanks in advance
>
> Sawan
>



--
-
P




--
-
P

Reply via email to