Thread: Question about signed and unsigned ints in c++

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    20

    Question about signed and unsigned ints in c++

    What are the largest signed and unsigned ints in c++?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The "sizes" of the integral types is implementation defined. To find the limits any of the integral types can hold for a given implementation you can use some of the members of the numeric_limits class.


    Jim

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    The specific sizes are implementation defined, but the standard does clearly state the minimum range for all of them. For int/unsigned int, the ranges are -32767 to 32767, and 0 to 65535, respectively. Int and unsigned int may have wider ranges than this, but this is the minimum that the standard requires. it's actually very common for the ranges to be -2,147,483,647 to 2,147,483,647 and 0 to 4,294,967,295 respectively.

    Source: Fundamental types - cppreference.com
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    May 2015
    Posts
    56
    Hello,

    Found this a long time ago, a quick runtime check.
    This works with gcc, don't know about the MS compiler.

    Code:
    unsigned int max = -1;
    printf("max = %u\n", max);//max = 4294967295

    Regards

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I would never rely on code like that lol.

    If you want to set all the bits of a region of memory, use memset.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by MutantJohn View Post
    I would never rely on code like that lol.

    If you want to set all the bits of a region of memory, use memset.
    Actually, that snippet contains guaranteed behavior, according to the standard. As for "setting all bits," the standard doesn't specify that, but unsigned int underflow is well-defined behavior.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Ah, it is indeed well-defined behavior. It's just not very self-documenting or clear code.

    It looks like they were using that behavior to set all the bits of an unsigned int to see its largest value which is why I suggested using memset instead. These kinds of things may work but they seem hackish to me in the sense that they don't convey intent and require a tab with the C++ standard open.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by MutantJohn View Post
    ...require a tab with the C++ standard open.
    You're supposed to just know it. :P
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on unsigned vs signed comparisons
    By CodeSlapper in forum C Programming
    Replies: 6
    Last Post: 03-08-2016, 06:24 PM
  2. Signed and unsigned
    By ncode in forum C Programming
    Replies: 3
    Last Post: 09-02-2011, 08:59 PM
  3. Replies: 2
    Last Post: 01-06-2009, 10:37 AM
  4. Signed unsigned ints?
    By Mostly Harmless in forum C++ Programming
    Replies: 11
    Last Post: 08-22-2008, 02:00 AM
  5. Signed and unsigned
    By alyeska in forum C++ Programming
    Replies: 5
    Last Post: 09-18-2007, 12:27 PM

Tags for this Thread