C++: digits vs bits?

334 views Asked by At

I am trying to understand the difference of vocabulary used in the C++ language between digits and bits as in :

CHAR_BIT;
std::numeric_limits<char>::digits;

Is there a conceptual difference? Maybe for weird architectures? If so, what would be called the result of the operator[] of std::bitset. Does it give access to a bit or to a digit?

And the current documentation of boost does not help: cppint provides a code with Digits but the documentation mention Bits (this is obviously a problem with the documentation, but I don't know whether the text or the code is more recent.)

1

There are 1 answers

1
Some programmer dude On BEST ANSWER

From this std::numeric_limits::digits reference:

The value of std::numeric_limits::digits is the number of digits in base-radix that can be represented by the type T without change. For integer types, this is the number of bits not counting the sign bit.

And later it states that for char the result is CHAR_BIT - std::numeric_limits<char>::is_signed.

And from the C numeric limits reference:

CHAR_BIT number of bits in byte

So for a normal modern computer, where char is eight bits, then CHAR_BITS is equal to 8 and the digits function will return either 7 or 8 depending on if char is signed or not.

I agree that the using the word "digits" is is kind of confusing when getting the number of bits in an integer type, but it makes much more sense for floating point types:

For floating-point types, this is the number of digits in the mantissa.


And std::bitset is a set of bits, so the indexing operator will give you the selected bit in the set.