First , Byte: Byte is a byte, and a bit is a bit
1 Byte = 8 bit
1 KB = 1024 Byte
1 MB = 1024 KB
1 GB = 1024 MB
After a brief understanding of bytes and bits, let's look at the simplest example, 8-bit-unsigned maximum and minimum values.
Each of them can be filled with 0 or 1,
1 bit The maximum value in binary is 1 The maximum value is 1 (2^1)-1
2 bits, the maximum value in binary is 11, the maximum value is 3 (2^2)-1
The maximum value of 3 bits in binary is 111, and the maximum value is 7 (2^3)-1
......
So if it is 8-bit unsigned, the maximum value is 1111 1111, and the maximum value is 255, which is (2^8)-1.
However, the sign is just to let the first of the 8 bits act as a sign bit on this basis, 0 represents a positive number, and 1 represents a negative number.
sign bit 0 positive, 1 negative |
It is equivalent to becoming 7 effective bits, then
The maximum value of 8-bit signed is 0111 1111, the maximum value is +127, which is (2^7)-1,
The minimum value of 8-bit signed is 1000 0000, the minimum value is -128, which is -2^7,
Note: The reason why it is -2^7 here instead of subtracting 1 is because the positive direction starts from 0, 0~127, a total of 128 numbers;
There is no 0 in the negative direction, because there is actually no distinction between positive 0 and negative 0, there is only one 0. If the negative direction also starts from 0, it is equivalent to counting 0 twice. In the computer, by default, 0 is assigned to the positive direction, so the negative direction starts from -1, which is 1000 0001, and -1 ~ -128 is also 128 numbers. In addition, by default, 1000 0000 (negative 0) is -128, because it is impossible to represent 128 with 7 significant digits.
So after having a simple foundation, we can get the maximum and minimum values of signed numbers corresponding to 2 bytes, 4 bytes, and 8 bytes respectively.
Signed number: 2 bytes (16 bits)
decimal | binary | ||
maximum value | (2^15)- 1 | 32767 | 01...1 //15 1s after 0 |
minimum value | - 2^15 | - 32768 | 10...0 //15 0s after 1 |
Signed number: 4 bytes (32 bits)
decimal | binary | ||
maximum value | (2^31)- 1 | 2147483647 | 01...1 //0 followed by 31 1s |
minimum value | - 2^31 | -2147483648 | 10...0 //31 0s after 1 |
Signed number: 8 bytes (64 bits)
decimal | binary | ||
maximum value | (2^63)- 1 | big numbers | 01...1 //0 followed by 63 1s |
minimum value | - 2^63 | big numbers | 10...0 //63 0s after 1 |
-----------------------------------------------------------------------------------------------
end of article