15

I have not researched it so I'm not sure if this is a widely known thing but I figured out a sort of hacky way to get a max integer value:

-Declare an unsigned int.
-Subtract 1
-Divide 2

That is your max signed int value

int main(){
unsigned int a = 0;
a--;
std::cout << a / 2 << std::endl;
}

Comments
  • 8
    In Cpp its as simple as

    #include <limits>
    int imax = std::numeric_limits<int>::max();
  • 9
    You depend on integer overflow being deterministic for that, and, if I recall correctly, it is undefined behavior, so it might not work on some platforms. Much more reliable to use the <limits> header, like @dontPanic suggested.
  • 3
    If you are able to work with the individual bits and you know the length of your datatype you could "construct" the value yourself. However I'd go with the limits too.
  • 1
    Aaand in Java simply:
    int aReallyBigInteger = Integer.MAX_VALUE;

    (For those that don't know/use Java int is a primitive type and Integer is object-based type)
  • 2
    @dontPanic @aritzh Yeah but that's not as cool
  • 2
    I thought this was common knowledge?
    I've been using unsigned int -1 For like fifteen years lol
    (Though it is technically undefined behavior, so maybe not that common? idk)
  • 1
    @Root I did say I didn't research it. It may be common knowledge.
Add Comment