Thread: Unsigned vs. Signed?

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Unsigned vs. Signed?

    Hi,

    is an unsigned integral type negative, and a signed integral type positive? Or is it the exact opposite? There was some confusion on which is which in the book I'm reading.
    Last edited by Programmer_P; 12-22-2009 at 01:10 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    unsigned implies that the possible values are non-negative; signed implies that the possible values can be negative.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Note that the MSB of the signed value is used to define if the value is negative or positive, also the value stored for signed types is cut in half to those values stored in an unsigned storage type. Food for thought.

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, thanks. One more question:

    Are all basic types, such as int, char, etc. signed by default?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    Are all basic types, such as int, char, etc. signed by default?
    No. short int, int and long int are indeed signed integer types, but whether char is signed or unsigned is implementation defined. Consequently, if you want a signed char, use a signed char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Heap corruption using zlib inflate
    By The Wazaa in forum C++ Programming
    Replies: 0
    Last Post: 03-29-2007, 12:43 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM