Analysis of binary and int range/IP computing and PHP applications

Source: Internet
Author: User
Brief introduction to binary and int range IP computing and PHP application

?? Everything in the programmer's world is binary.

PHP integer length and binary principle

?? We have always said that integer data has 32 bits. many people say that integer data has a maximum value of 2 ^ 32-1. in fact, this is wrong. An integer occupies four bytes and each byte has 8 bits. assume that an integer 3 can be expressed:
In this case, the eight digits are separated to facilitate observation. in the true sense, it is continuous.
00000000 00000000 00000000 00000011
SUM = 2 ^ 0 + 2 ^ 1 = 3
How does one represent a positive or negative number? Indicates the number of signed integers. if the value is 0, the value 0 indicates positive and 1 indicates negative.
So-1 indicates
10000000 00000000 00000000 00000011
The maximum positive integer can be expressed in this way.
01111111 11111111 11111111 11111111
SUM = 2 ^ 0 + 2 ^ 1 + 2 ^ 2 ·······
It can be found that this is our high school mathematics, calculate the first N of the proportional series and
Review the formula below:
Sn = (1-q ^ n)/(1-q)
The formula above shows that the public ratio is 2 and there are 31 items.
Sn = 2 ^ 31-1
Remember that the maximum value of a signed integer is 2 ^ 31-1 = 2147483647
The minimum value is-2147483648. if you are interested in research, you can use Baidu.

IP address calculation

?? According to common sense, each IP address must be in the range of 0. It is clear according to its binary.
For example, IP 127.0.0.1
01111111 00000000 00000000 00000001
Each IP address range can only be in the [limit, 11111111]
Calculate the formula, 255

Applications in development
  • Interchange of IP strings with integer types
    PHP is really a good thing. it helps us get two functions ip2long (), long2ip (), and the code is talking:
 

The three var_dump types are int, string, and int.
This application is very practical, that is, saving SQL.
1. an integer uses less space than a string.
2. for SQL queries, the numeric efficiency is higher than the string.
There are many others, and we look forward to hearing from other students.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.