# Thread: unsigned char vs signed char and range of values

1. ## unsigned char vs signed char and range of values

I'm just making sure I understand how this works. If you have a byte repesenting an unsigned value (unsigned assumes absolute) then you can have 256 different values (2 raised to the 8) with a range of (0 - 255).

Now if you have a signed char, 2's compliment system makes it so that all of the non negative values can have both positive and negative values, correct? What is the maximum absolute value of a signed char (or any variable that is only a single byte). If you divide 256 / 2 you get 128, does that mean (0-127) is the range of values?

I'm pretty sure I understand this stuff, but I've been having problems with standards and file IOstreams and I'm wondering if something like this may be the source of the error.

thanks

2. >>If you have a byte repesenting an unsigned value (unsigned assumes absolute) then you can have 256 different values
Not necessarily, a byte doesn't have to be 8 bits. The largest value an unsigned char can hold is UCHAR_MAX in <climits>, the smallest is 0. For a signed char, the largest is SCHAR_MAX and the smallest is SCHAR_MIN.

3. Hmm Okay. When and why would a byte not be 8 bits?

4. >>When and why would a byte not be 8 bits?
Different machine architectures may choose to use a different byte size. A common size is 8, called an octet, but C++ only guarantees that the number of bits in a char will be at least 8, the size of a char is mapped directly to the byte size of the hardware. If you use the stuff in <limits> or <climits> then it won't matter what your machine uses because your programs will use portable names and functions when you need that kind of information.

5. ## Re: unsigned char vs signed char and range of values

Originally posted by Silvercord
I'm just making sure I understand how this works. If you have a byte repesenting an unsigned value (unsigned assumes absolute) then you can have 256 different values (2 raised to the 8) with a range of (0 - 255).

Now if you have a signed char, 2's compliment system makes it so that all of the non negative values can have both positive and negative values, correct? What is the maximum absolute value of a signed char (or any variable that is only a single byte). If you divide 256 / 2 you get 128, does that mean (0-127) is the range of values?
An unsigned char is GUARANTEED to hold at least 0-255
A signed char is GUARANTEED to hold numbers from -127 to +127

In practice, with the 2's complimentary system and an 8-bit signed char results in the range -128 to +127 (an extra value which is not required who's 2's compliment is itself, similar to 0, -128).

A byte doesn't have to be 8-bits, however, we know that in practice it has to be at least 8-bits because of the ranges a char has to be capable of representing.

A char is neither defined to be signed or unsigned, it's implementation dependant.

6. Cool, I thank you both very much .

Popular pages Recent additions