Thread: unsigned char vs signed char and range of values

  1. #1
    Banned
    Join Date
    Jan 2003
    Posts
    1,708

    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. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>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.
    *Cela*

  3. #3
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Hmm Okay. When and why would a byte not be 8 bits?

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>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.
    *Cela*

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    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. #6
    Banned
    Join Date
    Jan 2003
    Posts
    1,708

    Thumbs up

    Cool, I thank you both very much .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  3. Cross platform portability, about data types...
    By gaah in forum C++ Programming
    Replies: 9
    Last Post: 01-21-2005, 10:32 PM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. unsigned and signed char
    By P.Phant in forum C Programming
    Replies: 3
    Last Post: 07-12-2003, 05:00 AM