delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/15/23:02:55

Date: Tue, 15 Jul 1997 23:02:39 -0400 (EDT)
Message-Id: <1.5.4.16.19970716083448.400f43a0@giasbga.vsnl.net.in>
Mime-Version: 1.0
To: eyal DOT ben-david AT aks DOT com, djgpp AT delorie DOT com
From: Chirayu Krishnappa <chirayu AT radiolink DOT net>
Subject: Re: odd or even # of 1's (was: no subject)

>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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019