Thread: How to tell a type is signed or unsigned?

  1. #16
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by KIBO View Post
    Does it still work when you're not on a platform using 2-complement??
    It doesn't depend at all on how the integer is represented, just the fact that the subtraction will produce SOME result.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    1) It's not UB. Underflow is defined for unsigned integers (wrap around), and there's no underflow for signed integers.

    2) However, that doesn't mean you can ignore over/underflow for signed integers in general. There are platforms that trap on overflow. There may also be platforms that have an integer NaN they'd use for overflow results. And if that's the case, the claim that the subtraction will produce SOME result is, well, only half-true. Operations on NaNs usually have weird results.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by CornedBee View Post
    1) It's not UB. Underflow is defined for unsigned integers (wrap around), and there's no underflow for signed integers.

    2) However, that doesn't mean you can ignore over/underflow for signed integers in general. There are platforms that trap on overflow. There may also be platforms that have an integer NaN they'd use for overflow results. And if that's the case, the claim that the subtraction will produce SOME result is, well, only half-true. Operations on NaNs usually have weird results.
    can you give an example of a platform that will trap on overflow

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    VAX with the IV status bit set.
    http://h71000.www7.hp.com/doc/73fina...15pro_014.html

    CLR in checked mode.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Short story:
    It's impossible to truly determine that.
    Wether singed or unsigned, the var is still meerly a specified amount of reserved memory. Being singed or unsigned only changes the behavior of the compiler's operations on that varible. That's all.

  6. #21
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by CornedBee View Post
    1) It's not UB. Underflow is defined for unsigned integers (wrap around)
    Can you direct me to the relevant section(s)? I'm having difficulty locating it.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Can you direct me to the relevant section(s)? I'm having difficulty locating it.
    The behaviour is defined in in paragraph 2 of section 4.7 of the 2003 edition of the C++ Standard (ISO/IEC 14882:2003).

    If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2 n where n is the number of bits used to represent the unsigned type). [Note: In a two's complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). ]
    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

  8. #23
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Thanks laserlight.

    Apologies, brewbuck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. Cross platform portability, about data types...
    By gaah in forum C++ Programming
    Replies: 9
    Last Post: 01-21-2005, 10:32 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM