Velocity Reviews > Use of bitwise operator for signed number

# Use of bitwise operator for signed number

arunaling@yahoo.co.in
Guest
Posts: n/a

 06-07-2006
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

CBFalconer
Guest
Posts: n/a

 06-07-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

Sriram Rajagopalan
Guest
Posts: n/a

 06-07-2006

(E-Mail Removed) 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

Chris Dollin
Guest
Posts: n/a

 06-07-2006
Sriram Rajagopalan wrote:

> (E-Mail Removed) 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/

Guest
Posts: n/a

 06-07-2006

Sriram Rajagopalan wrote:
> (E-Mail Removed) 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;

Flash Gordon
Guest
Posts: n/a

 06-07-2006
(E-Mail Removed) 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

pete
Guest
Posts: n/a

 06-07-2006
(E-Mail Removed) 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

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Rob1bureau VHDL 1 02-27-2010 12:13 AM Ioannis Vranos C++ 8 11-14-2008 11:03 PM Manish_Ganvir C Programming 13 02-14-2005 07:24 PM codefixer@gmail.com C Programming 3 02-04-2005 07:03 AM Roland Reichenberg Perl Misc 7 10-14-2003 01:05 PM