I can never remember the number. I need a memory rule.
I can never remember the number. I need a memory rule.
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.
Rather than think of it as one big number, try breaking it down and looking for associated ideas eg:
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!
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:
Just take any decent calculator and type in "7FFFFFFF" in hex mode, then switch to decimal.
2147483647.
32 bits, one for the sign, 31 bits of information:
2^31 - 1 = 2147483647
Why -1?
Because the first is zero, so the greatest is the count minus one.
EDIT for cantfindaname88
The count is 2^31 but the greatest can't be 2147483648 (2^31) because we count from 0, not 1.
Rank 1 2 3 4 5 6 ... 2147483648
Number 0 1 2 3 4 5 ... 2147483647
Another explanation with only 3 bits : 1 for the sign, 2 for the information
2^2 - 1 = 3
Below all the possible values with 3 bits: (2^3 = 8 values)
1: 100 ==> -4
2: 101 ==> -3
3: 110 ==> -2
4: 111 ==> -1
5: 000 ==> 0
6: 001 ==> 1
7: 010 ==> 2
8: 011 ==> 3
Well, it has 32 bits and hence can store 2^32 different values. Half of those are negative.
The solution is 2,147,483,647
And the lowest is −2,147,483,648.
(Notice that there is one more negative value.)
The easiest way to remember is to look at std::numeric_limits< int >::max()
For example (from MSDN),
// numeric_limits_max.cpp
#include <iostream>
#include <limits>
using namespace std;
int main() {
cout << "The maximum value for type float is: "
<< numeric_limits<float>::max( )
<< endl;
cout << "The maximum value for type double is: "
<< numeric_limits<double>::max( )
<< endl;
cout << "The maximum value for type int is: "
<< numeric_limits<int>::max( )
<< endl;
cout << "The maximum value for type short int is: "
<< numeric_limits<short int>::max( )
<< endl;
}
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.
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.
In Objective-C (iOS & OSX), just remember these macros:
#define INT8_MAX 127
#define INT16_MAX 32767
#define INT32_MAX 2147483647
#define INT64_MAX 9223372036854775807LL
#define UINT8_MAX 255
#define UINT16_MAX 65535
#define UINT32_MAX 4294967295U
#define UINT64_MAX 18446744073709551615ULL
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!
With Groovy on the path:
groovy -e " println Integer.MAX_VALUE "
(Groovy is extremely useful for quick reference, within a Java context.)
2147483647
Here's what you need to remember:
Hence 2, 147, 483, 647
I made a couple genius methods in C# that you can take advantage of in your production environment:
public static int GetIntMaxValueGenius1()
{
int n = 0;
while (++n > 0) { }
return --n;
}
public static int GetIntMaxValueGenius2()
{
int n = 0;
try
{
while (true)
n = checked(n + 1);
}
catch { }
return n;
}
"If a huge integer isn't recalled, you recall this mnemonic."
Now count the letters in each word.
It's 231 − 1 (32 bits, one is used for sign).
If you want an approximate value, use 210 = 1024 ≈ 103, so 231 ≈ 2*109. If you want to compute an exact value by hand, use exponentiation by squaring to get to 232 = 2(25) and divide by two. You only need to square five times to get 232:
2*2 = 4
4*4 = 16
16*16 = 256
256*256 = 25*25*100 + 2*250*6 + 36 = 62500 + 3000 + 36 = 65536
65536*65536 =65000*65000 + 2*65000*536 + 536*536 =
4225000000 + 130000*536 + (250000 + 3600 + 36*36) =
4225000000 + 69680000 + 250000 + 3600 + 1296 =
4294967296
dividing this by two and subtracting one gives you 2,147,483,647. If you don't need all digits, but only want say, the first three significant digits, the computations on each squaring step are very easy.