Use of bitwise operator for signed number

A

arunaling

Hi all,

I have the following situation. I want to use the following piece of
code to count the number of bits in an integer I use the following
piece of code.

void bitcount(int k)
{
for (int count =0;k; k&=k-1)
count++;
}

How do I make sure that this piece of code works for unsigned integers
too. Is there any way to do that.

Thanx and Regards,
Aruna
 
C

CBFalconer

I have the following situation. I want to use the following piece
of code to count the number of bits in an integer I use the
following piece of code.

void bitcount(int k)
{
for (int count =0;k; k&=k-1)
count++;
}

How do I make sure that this piece of code works for unsigned
integers too. Is there any way to do that.

It won't work for anything, since the moment the for loop is exited
count goes out of scope. However:

int bitcount(unsigned int k)
{
int count;

for (count = 0; k; k &= k-1) count++;
return count;
}

should work when called with either int or unsigned int. You have
to decide what it means for the various possible representations of
negative values.

--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." --Hermann Goering.
 
S

Sriram Rajagopalan

Hi all,

I have the following situation. I want to use the following piece of
code to count the number of bits in an integer I use the following
piece of code.

void bitcount(int k)
{
for (int count =0;k; k&=k-1)
count++;
}

The above piece of code would not count the number of bits in an
integer.
Instead it would be better to use the operator sizeof to get the job
done as below:

int numOfBitsInInt = sizeof(int) * 8;

Similarly:

int numOfBitsInUint = sizeof(unsigned int) * 8;
 
C

Chris Dollin

Sriram said:
The above piece of code would not count the number of bits in an
integer.

Looks OK to me.
Instead it would be better to use the operator sizeof to get the job
done as below:

int numOfBitsInInt = sizeof(int) * 8;

Ah, no. The OP wants to count the number of /set/ bits in an integer,
not the number of bits /available/ in an integer.

Your answer isn't portable anyway: it assumes that char has 8 bits.
 
V

Vladimir Oka

Sriram said:
The above piece of code would not count the number of bits in an
integer.
Instead it would be better to use the operator sizeof to get the job
done as below:

int numOfBitsInInt = sizeof(int) * 8;

Similarly:

int numOfBitsInUint = sizeof(unsigned int) * 8;

Only on a system with 8 bit byte, nowhere as universal as you may
think.

Better to use CHAR_BIT from <limits.h> instead, e.g.:

int numOfBitsInUint = sizeof(unsigned int) * CHAR_BIT;
 
F

Flash Gordon

Hi all,

I have the following situation. I want to use the following piece of
code to count the number of bits in an integer I use the following
piece of code.

void bitcount(int k)
{
for (int count =0;k; k&=k-1)
count++;
}

How do I make sure that this piece of code works for unsigned integers
too. Is there any way to do that.

First worry about whether it works for signed numbers. k-1 will overflow
if you pass in INT_MIN, and the behaviour on overflow is undefined.
 
P

pete

Hi all,

I have the following situation. I want to use the following piece of
code to count the number of bits in an integer I use the following
piece of code.

void bitcount(int k)
{
for (int count =0;k; k&=k-1)
count++;
}

How do I make sure that this piece of code works for unsigned integers
too. Is there any way to do that.

unsigned bit_count(unsigned n)
{
unsigned count;

for (count = 0; n != 0; n &= n - 1) {
++count;
}
return count;
}
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top