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

1. ## 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. 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

3. How do I get printf to display the value?

4. Code:
```printf("%lu", ULONG_MAX);

printf("%lu", -1UL);```
--
Mats

5. Originally Posted by matsp
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);`

6. > 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.

7. Originally Posted by matsp
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.)

8. Originally Posted by brewbuck
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.

9. 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.

10. Originally Posted by robatino
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.

11. > 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.

12. Code:
`printf("%lu", ~0UL);`
Can someone explane how this is working? Seems like this could be really handy to understand.

13. "&#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

14. Originally Posted by robatino
> 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.