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

D

Dale Dellutri

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.
 
R

Rich Webb

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?
 
D

Dale Dellutri

On Thu, 2 Apr 2009 15:31:41 +0000 (UTC), Dale Dellutri
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.
 
K

Kaz Kylheku

^^
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?
 
A

Andrey Tarasevich

Dale said:
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.
 
V

vippstar

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.
 
J

James Kuyper

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).
 
V

vippstar

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.
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.
 
J

James Kuyper

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.
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.
 
D

dfighter

Dale said:
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.
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top