tags:

views:

241

answers:

5

How do I get the number of bits in type char?

I know about CHAR_BIT from climits. This is described as »The macro yields the maximum value for the number of bits used to represent an object of type char.« at Dikumware's C Reference. I understand that means the number of bits in a char, doesn't it?

Can I get the same result with std::numeric_limits somehow? std::numeric_limits<char>::digits returns 7 correctly but unfortunately, because this value respects the signedness of the 8-bit char here…

+1  A: 

If you want to be overly specific, you can do this:

sizeof(char) * CHAR_BIT

If you know you are definitely going to do the sizeof char, it's a bit overkill as sizeof(char) is guaranteed to be 1.

But if you move to a different type such as wchar_t, that will be important.

R Samuel Klatchko
A: 

How about...

int num_bits_in_char = sizeof(char)*8;
Jeremy Friesner
Nothing says a byte has to contain 8 bits.
anon
According to the ISO C std (6.5.3.4, "The sizeof operator", part 3), "When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1". So this is just saying "int num_bits_in_char = 8;".
Ken
@Ken: No, these definitions are not equivalent. While a **byte** (ie. the smallest addressable unit of memory) today is normally equal to an **octet** (which is guaranteed to be exactly 8 bits in size) on _most_ hardware platforms, this doesn't have to be so on _all_ hardware platforms.
stakx
Out of curiosity, are there any modern platforms where a byte is not 8 bits?
Jeremy Friesner
stakx: I never said (nor meant to imply I thought) otherwise. I said that the C spec says sizeof(char)=1 always, and therefore sizeof(char)*8 = 8. I never made any statement about how many bits are in a byte.
Ken
A: 

A non-efficient way:

char c;
int bits;
for ( c = 1, bits = 0; c; c <<= 1, bits++ )
   ;
printf( "bits = %d\n", bits );
Mark Wilkins
+4  A: 

CHAR_BIT is, by definition, number of bits in the object representation of type [signed/unsigned] char.

numeric_limits<>::digits is the number of non-sign bits in the value representation of the given type.

Which one do you need?

If you are looking for number of bits in the object representation, then the correct approach is to take the sizeof of the type and multiply it by CHAR_BIT (of course, there's no point in multiplying by sizeof in specific case of char types, since their size is always 1, and since CHAR_BIT by definition alredy contains what you need).

If you are talking about value representation then numeric_limits<> is the way to go.

For unsigned char type the bit-size of object representation (CHAR_BIT) is guaranteed to be the same as bit-size of value representation, so you can use numeric_limits<unsigned char>::digits and CHAR_BIT interchangeably, but this might be questionable from the conceptual point of view.

AndreyT
Well, I forgot to mention why I was not content with using CHAR_BIT: I would like to get the number of bits in some templated code, and numeric_limits<> would fit in better in my first view. But the approach of using sizeof(char) * CHAR_BIT as mentioned by R Samuel Klatchko does the job.
mkluwe
A: 
tommieb75