Thread: Any suggestions for finding the largest possible value of INT?

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    23

    Any suggestions for finding the largest possible value of INT?

    In this case I am trying to figure out in code of the largest possible value of an unsigned long int. The problem is that if I just do the math 2^(whatever) i am bound to exceed the size of the value and as such can not return it. Also, is that the larges possible int value?

    What I really want to do is just set all the bits to 1 and print that but I don't know how to go in at that level. Also, would %d work for printing a Unsigned Long int or do I use something else like %lo

    Thanks for any help!

  2. #2

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The values are, as MacGyver says, in limits.h, e.g. ULONG_MAX.

    To find the largest unsigned number, you can actually use -1, as that is all ones. It is legal in C to set a unsigned value to -1 (don't ask me WHY this is allowed).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    How do I get printf to display the value?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    printf("%lu", ULONG_MAX);
    
    printf("%lu", -1UL);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    Code:
    printf("%lu", -1UL);
    That'll only work if -1 is a bit pattern of all 1's. Which is it, on any realistic 2's complement machine. But not in general.

    A good way to get a bit pattern of all 1's is to boolean negate the value 0:

    Code:
    printf("%lu", ~0UL);

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > That'll only work if -1 is a bit pattern of all 1's. Which is it, on any realistic 2's complement machine. But not
    > in general.

    I think it does work in general, and that two's complement vs. ones' complement vs. sign-magnitude only affects signed integral arithmetic (someone correct me if I'm wrong). I've seen this abbreviated notation used by people who are well-versed in this type of thing.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by matsp View Post
    It is legal in C to set a unsigned value to -1 (don't ask me WHY this is allowed).
    Probably along the same line of unsigned overflow and underflow being well-defined. (Interesting related thread.)
    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.*

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Quote Originally Posted by brewbuck View Post
    That'll only work if -1 is a bit pattern of all 1's. Which is it, on any realistic 2's complement machine. But not in general.

    A good way to get a bit pattern of all 1's is to boolean negate the value 0:

    Code:
    printf("%lu", ~0UL);
    That is very helpful as are many of the suggestions. I guess I really don't understand stdbool. The man page it not particularly illuminating.

    Thanks for the help.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If you read Dave_Sinkula's link, it turns out that -1UL and ~0UL both work portably when assigning to an unsigned long. However,
    Code:
    unsigned long i = -1;
    works portably, while
    Code:
    unsigned long i = ~0;
    does not. This is basically because ~0 is the bitwise complement of _signed_ 0, which depends on which of the three possible types of signed integral arithmetic the machine uses. You have to use the unsigned long version of zero, namely 0UL.

    Edit: Also, if you use ~0UL or -1UL, it only works portably for assigning to the same exact unsigned integral type, namely unsigned long. If you tried assigning to an unsigned int, as in
    Code:
    unsigned int i = ~0UL;
    this would be wrong unless unsigned int and unsigned long happen to have the same maximum value. So if the value needs to be assigned to a variable, it's safer to rely on the implicit cast, and this only works portably with -1, as noted above.
    Last edited by robatino; 09-05-2007 at 09:37 PM.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by robatino View Post
    You have to use the unsigned long version of zero, namely 0UL.
    Which I did...

    Edit: Also, if you use ~0UL, it only works portably for assigning to the same exact unsigned integral type, namely unsigned long.
    I'm not sure how that's a problem in this case.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > I'm not sure how that's a problem in this case.

    Sorry, I was somewhat confused and was in the middle of editing my edit when you posted. If you need the maximum value of a specific unsigned integral type, either form works as long as the proper suffix is applied. But the implicit-cast version only works using -1, since with 0 the cast has to be applied _before_ applying ~. It doesn't really matter unless you have a variable of some unsigned integral type and you might change it to a different unsigned integral type later, in which case it's easier to change
    Code:
    unsigned int i = -1;
    to
    Code:
    unsigned long i = -1;
    than to change
    Code:
    unsigned int i = ~0U;
    to
    Code:
    unsigned long i = ~0UL;
    But most of the time this won't matter. I spent some time thinking about it because I currently have some C++ code with a templated unsigned integral type T and was able to avoid using <limits> and std::numeric_limits<T>::max() by just using -1 instead.

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Code:
    printf("%lu", ~0UL);
    Can someone explane how this is working? Seems like this could be really handy to understand.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    "&#37;lu" specifies that the corresponding argument (the ~0UL) is an unsigned long - you have to specify the correct type.

    Edit: http://www.cppreference.com/stdio/printf.html
    Last edited by robatino; 09-05-2007 at 10:33 PM.

  15. #15
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Quote Originally Posted by robatino View Post
    > I'm not sure how that's a problem in this case.

    Sorry, I was somewhat confused and was in the middle of editing my edit when you posted. If you need the maximum value of a specific unsigned integral type, either form works as long as the proper suffix is applied. But the implicit-cast version only works using -1, since with 0 the cast has to be applied _before_ applying ~. It doesn't really matter unless you have a variable of some unsigned integral type and you might change it to a different unsigned integral type later, in which case it's easier to change
    Code:
    unsigned int i = -1;
    to
    Code:
    unsigned long i = -1;
    than to change
    Code:
    unsigned int i = ~0U;
    to
    Code:
    unsigned long i = ~0UL;
    But most of the time this won't matter. I spent some time thinking about it because I currently have some C++ code with a templated unsigned integral type T and was able to avoid using <limits> and std::numeric_limits<T>::max() by just using -1 instead.

    Where is the ~0UL coming from, is it part ot stdbool or something else. Thanks for the reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM