I can never remember that number. I need a memory rule.
Well it has 32Bits and hence can store 2^32 different values. Half of those are negative. Do the maths.
That's a simple memory rule if you have a good calculator. :)
The solution btw is: +2,147,483,647
and the lowest ist −2,147,483,648
(notice that there is one more negative)
It's 2,147,483,647. Easiest way to memorize it is via a tattoo.
It's 10 digits, so pretend it's a phone number (assuming you're in the US). 214-748-3647. I don't recommend calling it.
The value works out to 2,147,483,647.
That's (2^32-1)/2 because Int32 has 32 bits and half of it's values are negative.
Or, if you live in the world of .NET, don't bother remembering the number, just use Int32.MaxValue.
Rather than think of it as one big number, try breaking it down and looking for associated ideas eg:
- 2 maximum snooker breaks (a maximum break is 147)
- 4 years (48 months)
- 3 years (36 months)
- 4 years (48 months)
The above applies to the biggest negative number; positive is that minus one.
Maybe the above breakdown will be no more memorable for you (it's hardly exciting is it!), but hopefully you can come up with some ideas that are!
Just take any decent calculator and type in "7FFFFFFF" in hex mode, then switch to decimal.
2147483647.
2^(x+y) = 2^x * 2^y
2^10 ~ 1,000
2^20 ~ 1,000,000
2^30 ~ 1,000,000,000
2^40 ~ 1,000,000,000,000
(etc.)
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
2^9 = 512
So, 2^31 (signed int max) is 2^30 (about 1 billion) times 2^1 (2), or about 2 billion. And 2^32 is 2^30 * 2^2 or about 4 billion. This method of approximation is accurate enough even out to around 2^64 (where the error grows to about 15%).
If you need an exact answer then you should pull up a calculator.
Handy word-aligned capacity approximations:
- 2^16 ~= 64 thousand // uint16
- 2^32 ~= 4 billion // uint32, IPv4, unixtime
- 2^64 ~= 16 quintillion (aka 16 billion billions or 16 million trillions) // uint64, "bigint"
- 2^128 ~= 256 quintillion quintillion (aka 256 trillion trillion trillions) // IPv6, GUID
Just remember that 2^(10*x) is approximately 10^(3*x) - you're probably already used to this with kilobytes/kibibytes etc. That is:
2^10 = 1024 ~= one thousand
2^20 = 1024^2 = 1048576 ~= one million
2^30 = 1024^3 = 1073741824 ~= one billion
Since an int uses 31 bits (+ ~1 bit for the sign), just double 2^30 to get approximately 2 billion. For an unsigned int using 32 bits, double again for 4 billion. The error factor gets higher the larger you go of course, but you don't need the exact value memorised (If you need it, you should be using a pre-defined constant for it anyway). The approximate value is good enough for noticing when something might be a dangerously close to overflowing.
with Groovy on the path:
groovy -e " println Integer.MAX_VALUE "
(Groovy is extremely useful for quick reference, within a Java context)
What do you mean? It should be easy enough to remember that it is 2^32. If you want a rule to memorize the value of that number, a handy rule of thumb is for converting between binary and decimal in general:
2^10 ~ 1000
which means 2^20 ~ 1,000,000
and 2^30 ~ 1,000,000,000
Double that (2^31) is rounghly 2 billion, and doubling that again (2^32) is 4 billion.
It's an easy way to get a rough estimate of any binary number. 10 zeroes in binary becomes 3 zeroes in decimal.
Int32 means you have 32 bits available to store your number. The highest bit is the sign-bit, this indicates if the number is positive or negative. So you have 2^31 bits for positive and negative numbers.
With zero being a positive number you get the logical range of (mentioned before)
+2147483647 to -2147483648
If you think that is to small, use Int64:
+9223372036854775807 to -9223372036854775808
And why the hell you want to remember this number? To use in your code? You should always use Int32.MaxValue or Int32.MinValue in your code since these are static values (within the .net core) and thus faster in use than creating a new int with code.
My statement: if know this number by memory.. you're just showing off!
Anyway, take this regex (it determines if string contains not negative Integer in decimal form that not greater than Int32.MaxValue)
"[0-9]{1,9}|[0-1][0-9]{1,8}|20[0-9]{1,8}|21[0-3][0-9]{1,7}|214[0-6][0-9]{1,7}|2147[0-3][0-9]{1,6}|21474[0-7][0-9]{1,5}|214748[0-2][0-9]{1,4}|2147483[0-5][0-9]{1,3}|21474836[0-3][0-9]{1,2}|214748364[0-7]{1,1}"
Maybe it would help you to remember
join this cool facebook group. notice that it is an unsigned int. that's why divide by two and subtract one. very easy to remember.
Interestingly, Int32.MaxValue has more characters than 2,147,486,647..
But then again, we do have code completion,
So I guess all we really have to memorize is Int3<period>M<enter>
, which is only 6 characters to type in visual studio.
If you think the value is too hard to remember in base 10, try base 2: 1111111111111111111111111111111