R
rajm2019
count the bits required to be altered while swaping values a and b
count the bits required to be altered while swaping values a and b
count the bits required to be altered while swaping values a and b
count the bits required to be altered while swaping values a and b
count the bits required to be altered while swaping values a and b
count the bits required to be altered while swaping values a and b
Posting the same question every hour will not get you more or faster
responses. However, it will increase the number of killfiles that
contain you name.
count the bits required to be altered while swaping values a and b
count the bits required to be altered while swaping values a and b
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
unsigned bit_count(unsigned n);
int main(void)
{
unsigned a, b, mask;
a = rand();
b = rand();
mask = a ^ b;
printf("a is %u\nb is %u.\n", a, b);
printf("%u bits would need to be flipped in each object\n"
"to swap their values.\n", bit_count(mask));
printf("%u ^ %u is %u\n", a, mask, a ^ mask);
printf("%u ^ %u is %u\n", b, mask, b ^ mask);
return 0;
}
unsigned bit_count(unsigned n)
{
unsigned count;
for (count = 0; n != 0; n &= n - 1) {
++count;
}
return count;
}
/* END new.c */
Why you are using unsigned int everywhere?
Why count is unsigned?
And why you XOR the inputs?
To avoid a leading bit to be interpreted as a negative valueShraddha said:Why you are using unsigned int everywhere?
Why not? After all there would never be a negative number of bits counted.Why count is unsigned?
To find the bits that are set differently in the 2 operandsAnd why you XOR the inputs?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.