I don't understand why $2, 147, 483, 647$ is the max number for a $32$-bit integer.
$8$ bits $= 1$ byte $32$ bits $= 4$ bytes
How is this calculated? $8^{32}$ is way over $2$ billion.
I don't understand why $2, 147, 483, 647$ is the max number for a $32$-bit integer.
$8$ bits $= 1$ byte $32$ bits $= 4$ bytes
How is this calculated? $8^{32}$ is way over $2$ billion.
On
Bits are the smallest unit of binary number system (like ones in decimal system). So if we have a 32-bit binary number, the largest possible binary number that can be written is $$(11111111111111111111111111111111)_2$$ which can be converted to decimal by the sum $$\sum_{i = 0}^{31}2^i = 2^{32}-1 = 4294967295$$
However, $2147483647$ is the largest integer in Two's Complement (signed in other words), which is represented by $$(01111111111111111111111111111111)_2$$ since first bit is called the sign bit.
On
I realize that an answer has already been accepted, but your comment in the accepted answer felt like things are still unclear. Perhaps I am wrong.
(Also, I cannot comment, because I don't have enough reputation, so I guess for the sake of that too.)
If you switch to binary mode :) then: 00000001 represents the value 1, 00000010 = 2, 00000100 = 4, 00001000 = 8 and so on. Two raised to the power of thirty-two is just the same, just a bit set to one in the 32nd position, which is the most significant bit of a 32-bit number:
10000000 00000000 00000000 00000000
If this 32-bit number we are talking about is unsigned, then the number in question represents the value of 4,294,967,296, so around 4 billion, the max value an unsigned 32-bit can represent. If it is signed, on the other hand, then it would represent a value of -2,147,483,648, i.e. a negative number. This is because the most significant bit of a signed number designates whether the value is negative (1) or positive (0).
This means that the binary number above, depending on whether it's signed or unsigned, can represent two different values.
So while we often just say the max value of a 32-bit number, we have to specify signed or unsigned, and then we have to make sure we keep it positive to have a max value. So for a signed 32-bit number we make it 2 raised to the power of 32 minus one:
10000000 00000000 00000000 00000000 - 1 = 01111111 11111111 11111111 11111111
As you can see, the result is not, and can no longer be, a negative number (because of the most significant bit, the sign bit, cannot be 1 if we want to represent a positive value) and all other less significant bits are set to one. This represents the value 2,147,483,647, which is the max value of a signed 32-bit number.
This same concept is true for any traditional 8-bit, 16-bit, 64-bit, etc. binary number. ;)
I hope I have at least contributed to your (or someone's) understanding.
A $32$ bit integer can be represented as $b_1b_2b_3\cdots b_{32}$, where all of these are bits (so they are either $0$ or $1$). There are $2^{32}$ possibilities for such integers.
If they are unsigned (i.e. always nonnegative), one can represent the integers $$0,1,\cdots,2^{32}-1.$$ If they are signed, one represents $$-2^{31},-\left(2^{31}-1\right),\cdots,-1,0,1,\cdots,2^{31}-2,2^{31}-1.$$