469,945 Members | 1,830 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

Bit size of (un)signed char

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
1 11220
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.

Similar topics

2 posts views Thread by LC's No-Spam Newsreading account | last post: by
4 posts views Thread by Alex Vinokur | last post: by
21 posts views Thread by Angel Lopez | last post: by
53 posts views Thread by Neo | last post: by
12 posts views Thread by leonard.guillaume | last post: by
7 posts views Thread by Joey Sabey | last post: by
9 posts views Thread by Ido Yehieli | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.