T
tfeldman21
On 32-bit platform,
I am working on getting how many bits equal to 1 without an if loop.
I am working on getting how many bits equal to 1 without an if loop.
On 32-bit platform,
I am working on getting how many bits equal to 1 without an if loop.
On 32-bit platform,
I am working on getting how many bits equal to 1 without an if loop.
Harald said:Use a for loop or a while loop, testing each bit until you have
no more bits to test. I'm not sure what an if loop would be like,
unless you're using goto.
On the lowest bit can equal one. The rest equal 2, 4, 8 ... etc.On 32-bit platform,
I am working on getting how many bits equal to 1 without an if loop.
CBFalconer said:Harald said:Use a for loop or a while loop, testing each bit until you have
no more bits to test. I'm not sure what an if loop would be like,
unless you're using goto.
I have no idea what an 'if loop' is.
[example]
which doesn't care how wide an int is.
Harald said:CBFalconer said:Harald said:(e-mail address removed) wrote:
On 32-bit platform, I am working on getting how many bits equal
to 1 without an if loop.
Use a for loop or a while loop, testing each bit until you have
no more bits to test. I'm not sure what an if loop would be like,
unless you're using goto.
I have no idea what an 'if loop' is.
[example]
which doesn't care how wide an int is.
And the way I mentioned does?
count = value < 0;
for (bit = 1; bit < INT_MAX / 2; bit *= 2)
if (value & bit)
count++;
It accepts ints of any width.
Harald said:Harald said:CBFalconer said:Harald van D?k wrote:
(e-mail address removed) wrote:
On 32-bit platform, I am working on getting how many bits equal
to 1 without an if loop.
Use a for loop or a while loop, testing each bit until you have
no more bits to test. I'm not sure what an if loop would be like,
unless you're using goto.
I have no idea what an 'if loop' is.
[example]
which doesn't care how wide an int is.
And the way I mentioned does?
count = value < 0;
count = 0;
for (bit = 1; bit < INT_MAX / 2; bit *= 2)
for (bit = 1; bit != 0; bit *= 2)
if (value & bit)
count++;
It accepts ints of any width.
The question asked for unsigned ints, but I posted an example for ints.
I have no idea what an 'if loop' is.
[example]
which doesn't care how wide an int is.
And the way I mentioned does?
count = value < 0;
for (bit = 1; bit < INT_MAX / 2; bit *= 2)
  if (value & bit)
    count++;
It accepts ints of any width.
CoL said:You also try recursion for the same....
~col
Harald said:Harald said:(e-mail address removed) wrote:
On 32-bit platform, I am working on getting how many bits equal
to 1 without an if loop.
Use a for loop or a while loop, testing each bit until you have
no more bits to test. I'm not sure what an if loop would be like,
unless you're using goto.
I have no idea what an 'if loop' is.
[example]
which doesn't care how wide an int is.
And the way I mentioned does?
count = value < 0;
for (bit = 1; bit < INT_MAX / 2; bit *= 2)
if (value & bit)
count++;
It accepts ints of any width.
CBFalconer said:But it needs to know the precise type. My version will work when
fed bytes, longs, shorts, etc. (with matching changes in the type
of value, but making value an unsigned long will cover them all,
except long long which may or may not be available).
I find code fragments that need no editing more useful.
CBFalconer said:Harald said:Harald van D?k wrote:
(e-mail address removed) wrote:
On 32-bit platform, I am working on getting how many bits equal
to 1 without an if loop.
Use a for loop or a while loop, testing each bit until you have
no more bits to test. I'm not sure what an if loop would be like,
unless you're using goto.
I have no idea what an 'if loop' is.
[example]
which doesn't care how wide an int is.
And the way I mentioned does?
count = value < 0;
for (bit = 1; bit < INT_MAX / 2; bit *= 2)
if (value & bit)
count++;
It accepts ints of any width.
But it needs to know the precise type. My version will work when
fed bytes, longs, shorts, etc. (with matching changes in the type
of value, but making value an unsigned long will cover them all,
except long long which may or may not be available).
I find code fragments that need no editing more useful.
Christopher said:What's wrong with the typical straightforward way?
int bs(unsigned long v)
{
int c;
for (c = 0; v; v >>= 1)
c += v & 1;
return c;
}
On 32-bit platform,
I am working on getting how many bits equal to 1 without an if loop.
To count bits? You're not in the business of performance eh?
CBFalconer said:I have no idea what an 'if loop' is.
unsigned int value, count;
value = whatever;
count = 0;
/* loop invariant: count + bitsinvalue = bitsinwhatever */
while (value) {
count++;
value = (value - 1) & value;
}
/* bitsinvalue = 0 */
which doesn't care how wide an int is.
dick said:this is an interview question.
i = 0;
while(var)
{
var &= ~(-var);
i++;
}
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.