How to tell a type is signed or unsigned?

This is a discussion on How to tell a type is signed or unsigned? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by KIBO Does it still work when you're not on a platform using 2-complement?? It doesn't depend at ...

  1. #16
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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,893
    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,893
    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
    1,690
    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.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  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
    22,271
    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). ]
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-12-2008, 12:02 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11: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, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21