By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Bit size of (un)signed char

P: n/a
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
Mar 14 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 14 Mar, 17:35, nicola <vitac...@dimi.uniud.itwrote:
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

Mar 14 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.