Use of bitwise operator for signed number

Discussion in 'C Programming' started by arunaling@yahoo.co.in, Jun 7, 2006.

1. Guest

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

, Jun 7, 2006

2. CBFalconerGuest

wrote:
>
> 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.

CBFalconer, Jun 7, 2006

3. Sriram RajagopalanGuest

wrote:
> 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;

> 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

Sriram Rajagopalan, Jun 7, 2006
4. Chris DollinGuest

Sriram Rajagopalan wrote:

> wrote:
>> 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.

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.

--
Chris "I wish PDP-10s-for-the-21st Century were in widespread use" Dollin
"Who do you serve, and who do you trust?" /Crusade/

Chris Dollin, Jun 7, 2006

Sriram Rajagopalan wrote:
> wrote:
> > 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;

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;

6. Flash GordonGuest

wrote:
> 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.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Flash Gordon, Jun 7, 2006
7. peteGuest

wrote:
>
> 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;
}

--
pete

pete, Jun 7, 2006