How can I know what is the maximum assignable value for a variable from the the type of "unsigned long int"?
Asked
Active
Viewed 1.2k times
10
-
Does this answer your question? [C++ variable types limits](https://stackoverflow.com/questions/1966893/c-variable-types-limits) – phuclv Dec 01 '19 at 11:54
3 Answers
27
The obvious way would be to use std::numeric_limits<unsigned long>::max();

Jerry Coffin
- 476,176
- 80
- 629
- 1,111
-
2
-
@jvriesem: much of the reason it exists is because the value could vary (but it must have at least 32- bit range, so the minimum allowed value is a tad over 4 billion). – Jerry Coffin Feb 18 '20 at 05:37
6
Another way to find out would be:
unsigned long int i = (unsigned long int) -1;
printf("%lu\n", i);

Jeremy Friesner
- 70,199
- 15
- 131
- 234
-
3doesn't work on 1's complement machines (where -0 would be the max) – TemplateRex Aug 18 '13 at 18:17
-
4@TemplateRex - `(unsigned long)-1` is always the largest value that can be represented in an `unsigned long`. Regardless of the underlying hardware, unsigned arithmetic "shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer." [basic.fundamental]/4. – Pete Becker Aug 18 '13 at 19:16
-
@PeteBecker but e.g. `(char)-1` in 1's complement would be 254, and `(char)-0` would be 255. The problem is not the modulo arithmetic, but the mapping of signed to unsigned. What am I missing here? – TemplateRex Aug 18 '13 at 19:36
-
That `(char)-1` is -1, and that `(char)-0` is 0. A number and its underlying representation are two different things. And `(unsigned)-1` is `(2^n)-1` by language definition. – Cat Plus Plus Aug 18 '13 at 19:54
-
Call me oldfashioned, but for some reason I prefer `(unsigned long)-1` to that `std::numeric_limits
::max()` monster... maybe, because it's so much shorter? or because it does not require one function call, two scope lookups and a template instanciation to read? or because it's plain, simple C? – cmaster - reinstate monica Aug 18 '13 at 20:17 -
@cmaster The C style expression requires exactly the detailed knowledge of the language standard that commentators above were confused about. Further, that knowledge is utterly inapplicable to signed types. When `char` may be signed or unsigned depending on the implementation, that's even more annoying. In contrast, the C++ style is self-describing and is general for all types. You could at least endorse `UINT_MAX` from `limits.h`/`climits` if you really want to avoid templates (which will be fully expanded and inlined at compile-time anyway). – Phil Miller Aug 18 '13 at 20:49
-
@Novelocrat Yes, UINT_MAX is the best, most self describing choice :-) However, if I want a number with all its bits set, I would probably simply say `~0`, with the adequate size suffix, which does not require any arcane knowledge above knowledge of the C-operators, and is even much shorter than the cast version. – cmaster - reinstate monica Aug 18 '13 at 20:59
-
All bits set is not the same as maximum value, as the question asked. – Phil Miller Aug 18 '13 at 21:02
-
1@TemplateRex - you're looking at how underflow might be simply handled, but the standard says what it **has** to do, and if that simple implementation doesn't do it, then the implementation is wrong. – Pete Becker Aug 18 '13 at 23:46
-
1@PeteBecker You are right, but actually the relevant Standard quote is [conv.integral/2]: 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). — end note ]. – TemplateRex Aug 19 '13 at 06:33
-
-
@Jeremy Friesner:And -1 in `...(unsigned long int) -1;` used for avoid `syntax error : ';'`. You can use `...(unsigned long int) -0;`.Thus `(unsigned long int) -1;` isn't Max of `unsigned int`.-O1 for your answer. – May 19 '14 at 19:06
-
1
4
In simple way:
unsigned long int i = -1;
std::cout << i;