473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 11554
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: LC's No-Spam Newsreading account | last post by:
I asked a couple of days ago about the following arrangement (simplified). I made some progress using netscape.security.PrivilegeManager.enablePrivilege but still have to ask some further help. ...
4
by: Alex Vinokur | last post by:
I have got two functions: void foo1(char* str) { // Stuff } void foo2(int size) { char* str;
21
by: Angel Lopez | last post by:
Sorry if I am too naive, but this is my first post... I have a binary variable (it can contain either a 0 or a 1). Is there any way I can define a data type that uses only 1 bit. So far I have...
53
by: Neo | last post by:
Hi All, Is that true that size of a byte not necessarily 8-bit? What the std. says? If that true, then what will the size of an int, i mean what sizeof(int) should return? On my machine...
6
by: dddddddd2444444 | last post by:
Hi,please help... It works fine when I define a 2-D array like char code. But it won't work when I try to define the array dynamically using a function. It just crashes. Does anyone know why?...
12
by: leonard.guillaume | last post by:
Hi guys, I use dynamic char arrays and I'm trying to get rid of the garbage in it. Let me show you the code and then I'll explain more in details. ...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
7
by: Joey Sabey | last post by:
Hey, I've got a bit of a strange problem caused by a situation I'm in. I'm writing a DLL for a friend of mine in C++, and he plans to use it in delphi. The DLL needs to return strings, and we doubt...
9
by: Ido Yehieli | last post by:
Hi, from what i've read (http://tigcc.ticalc.org/doc/ keywords.html#short) and unsigned int should normally be in the range 0 to 65535. However, compiling and running this program: #include...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.