signed or unsigned?

This is a discussion on signed or unsigned? within the C Programming forums, part of the General Programming Boards category; If I wanted a number that went from 0 to 10,000, which would be the better option (for speed, effeciency, ...

  1. #1
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582

    signed or unsigned?

    If I wanted a number that went from 0 to 10,000, which would be the better option (for speed, effeciency, etc.) and why?

    signed short SSExample = 0;
    unsigned short USExample = 0;
    signed int SIExample = 0;
    unsigned int UIExample = 0;

    I'm just curious. In order of best to worst, which goes where?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well it fits in a short, so save 2 bytes... I'd use an signed short int in this case, for example if I wanted to flag an error I could set SSExample to -1. But if you don't plan on using it for that, use an unsigned short int

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Considering the target is most likely a PC, I might go with the following for speed:
    Code:
    short SSExample = 0; /*4 */
    unsigned short USExample = 0; /* 3 */
    int SIExample = 0; /* 2 */
    unsigned int UIExample = 0; /* 1 */
    And for size:
    Code:
    short SSExample = 0; /*2 */
    unsigned short USExample = 0; /* 1 */
    int SIExample = 0; /* 4 */
    unsigned int UIExample = 0; /* 3 */
    When signedness doesn't matter, always prefer unsigned (just because*). When size matters, choose the appropriate size. When speed matters, favor the compiler's favorite choice. When other platforms matter, make a different choice. Avoid short types, default promotions are not an enjoyable consideration.

    *Or perhaps because the math is generally more well-defined.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by zacs7 View Post
    Well it fits in a short, so save 2 bytes... I'd use an signed short int in this case, for example if I wanted to flag an error I could set SSExample to -1. But if you don't plan on using it for that, use an unsigned short int
    If SSExample is an unsigned short, you can use (unsigned short)(-1) (== 65535 for a 16-bit unsigned short) as the flag.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by robatino View Post
    If SSExample is an unsigned short, you can use (unsigned short)(-1) (== 65535 for a 16-bit unsigned short) as the flag.
    I'd rather not do that, since it avoids confusion this way... Most if not all of the standard C funcs are set out in this manner.

  6. #6
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    So, basically, unsigned is better and faster for speed?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by ulillillia View Post
    So, basically, unsigned is better and faster for speed?
    I'll take the 5th, or rather, claim that is at least as well-defined; I'll leave the rest to the implementors for the moment.

    Or make assumptions and go with "yes".
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Just use "int". It's not the shortest to type just by accident.
    It is made to be the shortest to type because it is most often the best choice in terms of performance etc, regardless of the register size of the machine.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-12-2008, 12:02 AM
  2. Heap corruption using zlib inflate
    By The Wazaa in forum C++ Programming
    Replies: 0
    Last Post: 03-29-2007, 01: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, 03:49 PM

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