Mail Archives: djgpp/1997/07/15/23:02:55
>Here are some functions to calculate the number of bits of an integer
>value.
>
>Method 1
>~~~~~~
>Twice faster than shift method (on avg) since the number of
>loops == the number of bits. (from Ratko Tomic)
>
>int bit_count_1 (long x)
>{
> int n = 0;
>/*
>** The loop will execute once for each bit of x set, this is in average
>** twice as fast as the shift/test method.
>*/
> if (x)
> do
> {
> n++;
> x &= (x-1);
> } while (x);
>
> return n;
>}
>
How can that be? Are'nt compares much slower than shifts. (The if(x) line).
How does it work?
>Method 2
>~~~~~~
>This Method is even faster than method 1.
>(Magic algorithm also by Ratko Tomic)
Thanks a lot. I'll check them out and get back to u.
>For more information about it look at SNIPPETS home page. This
>is a huge collection of useful C code fragments, functions and programs.
>
>http://www.brokersys.com/snippets/
>
Will check it out. Thanks again. Wonder why a search engine never turns up
with such sites!
Bye,
Chirayu Krishnappa:
------------------
e-mail: chirayu AT poboxes DOT com
Phone: 91 080 3332616.
============================================================================
==========
- Raw text -