Objective
?? In the programmer's world, everything is binary.
PHP integer length and binary principle
?? We have always said that the integral type has 32, many people say that the integer number is the biggest 2^32-1, in fact this is wrong. An integer is four bytes, each byte has 8bit, assuming an integer 3 can be expressed as:
Here for the convenience of observation, eight-bit separated, in the true sense, it is continuous.
00000000 00000000 00000000 00000011
SUM = 2^0 + 2^1 = 3
So how do you mean positive negative numbers? specified, signed integer number, 32nd digit 0 is positive, 1 is negative
So, 1, that means
10000000 00000000 00000000 00000011
The maximum positive integer can be expressed as such.
01111111 11111111 11111111 11111111
SUM = 2^0 + 2^1 + 2^2
It turns out that this has become our high school math, asking for geometric series before N and
Relive the formula:
Sn = (1-q^n)/(1-Q)
The previous formula shows that the male ratio is 2 and has 31 items.
Sn = 2^31-1
So remember, the signed integer maximum value is 2^31-1 = 2147483647
The minimum value is-2147483648, interested in research can be Baidu.
Calculation of IP Address
?? According to common sense, the area of IP for each bit is only in the 0-255 interval. It is clear according to its binary.
such as IP 127.0.0.1
01111111 00000000 00000000 00000001
IP each interval can only be in [00000000,11111111]
Set formula to calculate, 255
Application in the development
- The interchange of IP strings with integral types
PHP is really a good thing to help us get two functions Ip2long (), Long2ip (), code to speak:
Three var_dump types are Int,string,int
The application of this, one thing is very real, that is to deposit SQL.
1. An integral type uses less space than a string of strings.
2.SQL query, the digital efficiency is higher than the string.
There are many others, looking forward to other students to add.