Portable/standard way of counting number of bits in variable?

Discussion in 'C Programming' started by Dale Dellutri, Apr 2, 2009.

  1. Is there a portable/standard way of counting the
    number of bits in a variable. The following for
    statement works on all the machines I've tried:

    #include <stdio.h>
    int main(void) {
    int n;
    unsigned long int a;

    for (n = 0, a = 1; a != 0; a <<= 1, n++) ;

    printf("unsigned long int has %d bits.\n", n);
    return 0;
    }

    Produces:
    unsigned long int has 32 bits.

    I don't want to depend on C standard built-in defines.
    I want to check this during program execution.

    --
    Dale Dellutri <> (lose the Q's)
     
    Dale Dellutri, Apr 2, 2009
    #1
    1. Advertising

  2. Dale Dellutri

    Rich Webb Guest

    On Thu, 2 Apr 2009 15:31:41 +0000 (UTC), Dale Dellutri
    <> wrote:

    >Is there a portable/standard way of counting the
    >number of bits in a variable.


    With CHAR_BIT from limits.h and the sizeof operator.

    [snip...snip...]
    >I don't want to depend on C standard built-in defines.
    >I want to check this during program execution.


    If you don't trust the compiler to get sizeof right, why believe that it
    will do anything else correctly?

    --
    Rich Webb Norfolk, VA
     
    Rich Webb, Apr 2, 2009
    #2
    1. Advertising

  3. On Thu, 02 Apr 2009 11:50:26 -0400, Rich Webb <> wrote:
    > On Thu, 2 Apr 2009 15:31:41 +0000 (UTC), Dale Dellutri
    > <> wrote:


    > >Is there a portable/standard way of counting the
    > >number of bits in a variable.


    > With CHAR_BIT from limits.h and the sizeof operator.


    > [snip...snip...]
    > >I don't want to depend on C standard built-in defines.
    > >I want to check this during program execution.


    > If you don't trust the compiler to get sizeof right, why believe that it
    > will do anything else correctly?


    Probably because I knew about sizeof but not CHAR_BIT.

    Thanks.

    --
    Dale Dellutri <> (lose the Q's)
     
    Dale Dellutri, Apr 2, 2009
    #3
  4. Dale Dellutri

    Kaz Kylheku Guest

    On 2009-04-02, Dale Dellutri <> wrote:
    ^^
    You are a day late.

    > unsigned long int has 32 bits.
    >
    > I don't want to depend on C standard built-in defines.
    > I want to check this during program execution.


    Why, do you suspect that this value can change at run-time?

    For what other purpose would you defer to run time that which you can compute
    at compile time?
     
    Kaz Kylheku, Apr 2, 2009
    #4
  5. Dale Dellutri wrote:
    > Is there a portable/standard way of counting the
    > number of bits in a variable.


    There's number of bits in the 'object representation' and there's number
    of bits in the 'value representation'. You need to decide first, which
    one you need.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Apr 2, 2009
    #5
  6. Dale Dellutri

    Guest

    On Apr 2, 9:27 pm, Andrey Tarasevich <>
    wrote:
    > Dale Dellutri wrote:
    > > Is there a portable/standard way of counting the
    > > number of bits in a variable.  

    >
    > There's number of bits in the 'object representation' and there's number
    > of bits in the 'value representation'. You need to decide first, which
    > one you need.


    There's no such thing as a value representation. The object
    representation of an integer has value bits, optional padding bits and
    a sign bit, if the integer is signed. Special case are expressions
    with unsigned char type, which can not have any padding bits.

    Operations on values ignore the representation and thus padding bits
    are not taken into account.
     
    , Apr 3, 2009
    #6
  7. Dale Dellutri

    James Kuyper Guest

    wrote:
    > On Apr 2, 9:27 pm, Andrey Tarasevich <>
    > wrote:
    >> Dale Dellutri wrote:
    >>> Is there a portable/standard way of counting the
    >>> number of bits in a variable.

    >> There's number of bits in the 'object representation' and there's number
    >> of bits in the 'value representation'. You need to decide first, which
    >> one you need.

    >
    > There's no such thing as a value representation. ...


    The standard disagrees: "If there are N value bits, each bit shall
    represent a different power of 2 between 1 and 2N-1, so that objects of
    that type shall be capable of representing values from 0 to 2N -1 using
    a pure binary representation; this shall be known as the value
    representation." (6.2.6.2p1).

    Oddly enough, since 6.2.6.2p1 only describes unsigned integers, the
    standard doesn't clarify whether sign bits should be considered to be
    part of the value representation - I think they should. Since the
    standard never uses the term again, this isn't any big problem. That's
    probably why the term is not marked as a definition by putting it in
    italics.

    ....
    > Operations on values ignore the representation and thus padding bits
    > are not taken into account.


    The bit-wise operators (~, |, &, ^, <<, and >>) are all described as
    operating on bits. Since those operations don't deal with padding bits,
    that means that the bits they refer to make up the value representation
    (at least for unsigned types - the terminology is unclear for signed types).
     
    James Kuyper, Apr 3, 2009
    #7
  8. Dale Dellutri

    Guest

    On Apr 3, 1:32 pm, James Kuyper <> wrote:
    > wrote:
    > > On Apr 2, 9:27 pm, Andrey Tarasevich <>
    > > wrote:
    > >> There's number of bits in the 'object representation' and there's number
    > >> of bits in the 'value representation'.

    >
    > > There's no such thing as a value representation.

    >
    > The standard disagrees: "If there are N value bits, each bit shall
    > represent a different power of 2 between 1 and 2N-1, so that objects of
    > that type shall be capable of representing values from 0 to 2N -1 using
    > a pure binary representation; this shall be known as the value
    > representation." (6.2.6.2p1).
    >
    > Oddly enough, since 6.2.6.2p1 only describes unsigned integers, the
    > standard doesn't clarify whether sign bits should be considered to be
    > part of the value representation - I think they should. Since the
    > standard never uses the term again, this isn't any big problem. That's
    > probably why the term is not marked as a definition by putting it in
    > italics.


    Ah, thanks. I haven't read that before, (value representation). I
    disagree though that 6.2.6.1p1 applies for signed integers or that the
    value representation includes the sign bit, because other rules
    described in 6.2.6.1p1 also do not apply for signed integers, for
    example, "each bit shall represent a different power of 2 between 1
    and 2N-1", while ones' complement -1 does not satisfy this condition.

    Therefore, either signed integers do not have a value representation,
    or the standard has a defect (incomplete). IMHO it is the latter.

    > > Operations on values ignore the representation and thus padding bits
    > > are not taken into account.

    >
    > The bit-wise operators (~, |, &, ^, <<, and >>) are all described as
    > operating on bits. Since those operations don't deal with padding bits,
    > that means that the bits they refer to make up the value representation
    > (at least for unsigned types - the terminology is unclear for signed types).


    I'm not sure if the terminology is unclear, but to my experience,
    surely smaller than yours, bit-wise operations on signed integers
    indicated a bug in the code, and I've never encountered a problem
    which required such operations on signed integers.
     
    , Apr 3, 2009
    #8
  9. Dale Dellutri

    James Kuyper Guest

    wrote:
    > On Apr 3, 1:32 pm, James Kuyper <> wrote:

    ....
    >> Oddly enough, since 6.2.6.2p1 only describes unsigned integers, the
    >> standard doesn't clarify whether sign bits should be considered to be
    >> part of the value representation - I think they should. Since the
    >> standard never uses the term again, this isn't any big problem. That's
    >> probably why the term is not marked as a definition by putting it in
    >> italics.

    >
    > Ah, thanks. I haven't read that before, (value representation). I
    > disagree though that 6.2.6.1p1 applies for signed integers or that the
    > value representation includes the sign bit, because other rules
    > described in 6.2.6.1p1 also do not apply for signed integers, for
    > example, "each bit shall represent a different power of 2 between 1
    > and 2N-1", while ones' complement -1 does not satisfy this condition.


    I think the sign bit should be part of the value representation, because
    it plays a part in determining the value that is being represented.

    > Therefore, either signed integers do not have a value representation,
    > or the standard has a defect (incomplete). IMHO it is the latter.


    I would consider the failure to specify a definition that applies to
    signed integers a defect, were it not for the fact that the standard
    never makes any use of the term, other than to define it.

    >> The bit-wise operators (~, |, &, ^, <<, and >>) are all described as
    >> operating on bits. Since those operations don't deal with padding bits,
    >> that means that the bits they refer to make up the value representation
    >> (at least for unsigned types - the terminology is unclear for signed types).

    >
    > I'm not sure if the terminology is unclear, but to my experience,
    > surely smaller than yours, bit-wise operations on signed integers
    > indicated a bug in the code, and I've never encountered a problem
    > which required such operations on signed integers.


    I would certainly agree that bit-wise operations should only be perform
    on unsigned types. However, except for the shift operators, it is not
    undefined behavior to apply them to signed integers, not even if the
    signed integer has a negative value. The results are non-portable; but
    not necessarily a bug.
     
    James Kuyper, Apr 3, 2009
    #9
  10. Dale Dellutri

    dfighter Guest

    Dale Dellutri wrote:
    > Is there a portable/standard way of counting the
    > number of bits in a variable. The following for
    > statement works on all the machines I've tried:
    >
    > #include <stdio.h>
    > int main(void) {
    > int n;
    > unsigned long int a;
    >
    > for (n = 0, a = 1; a != 0; a <<= 1, n++) ;
    >
    > printf("unsigned long int has %d bits.\n", n);
    > return 0;
    > }
    >
    > Produces:
    > unsigned long int has 32 bits.
    >
    > I don't want to depend on C standard built-in defines.
    > I want to check this during program execution.
    >

    I understand that this is an assignment in colleges/universities, since
    I had to do this one too.
    You are doing just fine, you should not have portability problem with
    this solution.
     
    dfighter, Apr 4, 2009
    #10
    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. sarmin kho
    Replies:
    2
    Views:
    831
    A. Lloyd Flanagan
    Jun 15, 2004
  2. Miki Tebeka
    Replies:
    1
    Views:
    447
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
  3. Frank Buss
    Replies:
    3
    Views:
    4,085
    Mike Treseler
    Jul 31, 2006
  4. Mack

    Count number of bits set in a number

    Mack, Sep 27, 2007, in forum: C Programming
    Replies:
    12
    Views:
    819
    Mark Bluemel
    Sep 28, 2007
  5. Replies:
    12
    Views:
    591
Loading...

Share This Page