Bit size of (un)signed char

N

nicola

Hi,
I'm a bit confused about the following:

#include <iostream>

int main (int argc, char** argv) {
std::cout << "Bits of unsigned char: " <<
std::numeric_limits<unsigned char>::digits << std::endl;
std::cout << "Bits of char: " <<
std::numeric_limits<char>::digits << std::endl;
std::cout << "Bits of signed char: " <<
std::numeric_limits<signed char>::digits << std::endl;
return 0;
}

prints, on my system:

Bits of unsigned char: 8
Bits of char: 7
Bits of signed char: 7

I read in the standard (3.9.1) that all the three variants "occupy the
same amount of storage and have the same alignment requirements; that
is, they have the same object representation. For character types, all
bits of the object representation participate in the value
representation". Shouldn't that mean that their size, in bits, must be
the same? Isn't numeric_limits<> supposed to return such size?

Going on, I read that "For unsigned character types, all possible bit
patterns of the value representation represent numbers. These
requirements do not hold for other types". Which I interpret as "for,
let's say, signed char, one bit may be used for the sign". Still, signed
char's size, in bits, should be the same as for unsigned char, shouldn't
it?

Nicola
 
G

Gavin Deane

Hi,
I'm a bit confused about the following:

#include <iostream>

int main (int argc, char** argv) {
std::cout << "Bits of unsigned char: " <<
std::numeric_limits<unsigned char>::digits << std::endl;
std::cout << "Bits of char: " <<
std::numeric_limits<char>::digits << std::endl;
std::cout << "Bits of signed char: " <<
std::numeric_limits<signed char>::digits << std::endl;
return 0;

}

prints, on my system:

Bits of unsigned char: 8
Bits of char: 7
Bits of signed char: 7

I read in the standard (3.9.1) that all the three variants "occupy the
same amount of storage and have the same alignment requirements; that
is, they have the same object representation. For character types, all
bits of the object representation participate in the value
representation". Shouldn't that mean that their size, in bits, must be
the same?
Yes

Isn't numeric_limits<> supposed to return such size?
No

Going on, I read that "For unsigned character types, all possible bit
patterns of the value representation represent numbers. These
requirements do not hold for other types". Which I interpret as "for,
let's say, signed char, one bit may be used for the sign". Still, signed
char's size, in bits, should be the same as for unsigned char, shouldn't
it?

You've misunderstood what std::numeric_limits<T>::digits returns.
Where T is a built-in integer type, as in your examples, it gives the
number of non-sign bits. So it's not surprising that it the value for
signed char is one less than the value for unsigned char.

Gavin Deane
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top