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;

    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, Apr 2, 2009
    1. Advertisements

  2. Dale Dellutri

    Rich Webb Guest

    With CHAR_BIT from limits.h and the sizeof operator.

    If you don't trust the compiler to get sizeof right, why believe that it
    will do anything else correctly?
    Rich Webb, Apr 2, 2009
    1. Advertisements

  3. Probably because I knew about sizeof but not CHAR_BIT.

    Dale Dellutri, Apr 2, 2009
  4. Dale Dellutri

    Kaz Kylheku Guest

    You are a day late.
    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
  5. 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.
    Andrey Tarasevich, Apr 2, 2009
  6. Dale Dellutri

    vippstar Guest

    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.
    vippstar, Apr 3, 2009
  7. Dale Dellutri

    James Kuyper Guest

    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." (

    Oddly enough, since 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

    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
  8. Dale Dellutri

    vippstar Guest

    Ah, thanks. I haven't read that before, (value representation). I
    disagree though that applies for signed integers or that the
    value representation includes the sign bit, because other rules
    described in 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.
    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.
    vippstar, Apr 3, 2009
  9. Dale Dellutri

    James Kuyper Guest

    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.
    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.
    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
  10. Dale Dellutri

    dfighter Guest

    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
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.