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

Source: Internet
Author: User

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.

  • 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.