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
    #1
    1. Advertising

  2. CBFalconer Guest

    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
    #2
    1. Advertising

  3. 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
    #3
  4. Chris Dollin Guest

    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
    #4
  5. Vladimir Oka Guest

    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;
    Vladimir Oka, Jun 7, 2006
    #5
  6. Flash Gordon Guest

    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
    #6
  7. pete Guest

    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
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    984
    Michael Mair
    Feb 4, 2005
  2. Manish_Ganvir
    Replies:
    13
    Views:
    1,570
    Keith Thompson
    Feb 14, 2005
  3. Ioannis Vranos
    Replies:
    8
    Views:
    332
    James Kanze
    Nov 14, 2008
  4. Rob1bureau
    Replies:
    1
    Views:
    804
    joris
    Feb 27, 2010
  5. Roland Reichenberg
    Replies:
    7
    Views:
    164
    Josef Möllers
    Oct 14, 2003
Loading...

Share This Page