I hope this is what you want...After much thought I still feel uneasy about
using two function calls per run. Is there any better method?? Meanwhile I
am thinking of a better solution.
ciao
--------------program code-----------------------------
#include<stdio.h>
unsigned int numbitset(unsigned int x)
{
unsigned int n=0;
for(;x>0;x>>=1) /* shift number right and test against set bit */
if( x & 01 )
{
n++;
}
return n;
}
int main(void)
{
int i,j;
printf("enter first number: ");
scanf("%d",&i);
printf("enter secon number: ");
scanf("%d",&j);
printf("difference of bits set in %d and %d =
%d\n",i,j,numbitset(i)-numbitset(j));
return 0;
}
-----Original Message-----
From: Anukool Lakhina <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
Date: Tuesday, April 06, 1999 5:27 AM
Subject: Another bit question
>Hello all,
>
>Given two 32-bit numbers, I want to decide which number has more bits set.
>And what the difference in the number of bits set is. So for example, if
>Num1=1110000...011 and Num2= 0000011...001, I'm trying to write an
efficient
>function numBitsSet() that returns 2 if Num1 has 2 more bits set than Num2
>and -2 if Num2 has 2 more bits set than Num1. In this case, it'll return 2.
>
>Yes, I can do this easily by looping through the two numbers and storing a
>counter etc etc. But I need an efficient way to do this - I'm trying to do
>this by using a combination of xors or something. Please email me if you
>have any suggestions.
>
>Thank you.
>anukool.