1) The IPV4 address is a 32-bit binary number, usually divided by 4 "8-bit binary number", for convenience, usually in the form of "dotted decimal" (a.b.c.d), where a,b,c,d are the decimal integers between 0~255, in addition, An IP address can also be represented by an integer t of 3, and the method of calculating T is based on a,b,c,d in dotted decimal:
T=a*256*256*256+b*256*256+c*256+d;
2) So how to find out the "dotted decimal" form of this IP address according to the T inverse? Title Description:
The length of the IP address is 32, which means that there is a 2^32-1 address. The IP address generally uses dotted decimal notation, such as "192.168.1.1". The IP address can also be represented directly by a 32-bit integer. This topic requires a given integer IP address to represent the hair, and convert it to dotted decimal form. As an example: 3232235777 0xc0a80101 0xc0=192,0xa8=168,0x01=1,0x01=1 192.168.1.1
There are two ways to do this, and we'll start by talking about how to use bit manipulation to solve this problem. IP address is essentially a 32-bit binary form, although said T is an integer, but its computer storage form is still binary form, using "bit operation" can easily get the IP address of four network segments (that is, 4 "8-bit binary number"), the code is as follows:
Public StaticString bitoperation () {Long Ipaddrlong=0l; Scanner Scanner=NewScanner (system.in); if(Scanner.hasnextlong ()) {Ipaddrlong=Scanner.nextlong (); } Longtest; StringBuilder SB=NewStringBuilder (); for(inti = 0; I <4; i++) {Test= (IPADDRLONG&0XFF);//When there is an inconsistency between the two operands of the operation, the left side of the operand with fewer bits is 0 until the number of digits is equalSb.insert (0, long.tostring (test));//the Insert () method of the Stringbuild is very useful//sb.append (long.tostring (test)); if(i<3) {Sb.insert (0, "."); } Ipaddrlong=ipaddrlong>>8;//Whether you move left or right, the operand is to the left of ">>" or "<<", the right side is the number of digits to be changed, and after moving,//does not change the contents of the original operand, but creates a new value. The left shift is to move the binary code of the operand to the specified number of bits, so that the number of bits to the right of the operand is lost, in order to guarantee from the operand//the number of digits is the same as the number of moves in the leftmost 0. } returnsb.tostring ();}
Unlike left shift, there are two right-shift operators: ">>" and ">>>". For ">>", the whole binary code of the operand is positioned right, and the left empty part is replaced with the original symbol bit of the operand. The ">>>" is an unsigned right-shift operator, and the left-hand part is always replaced with 0.
Reference: http://www.what21.com/programming/java/java-algorithm/ip.html
3) There is another way to convert the IP address of the positive form into a "dotted decimal" form, mainly to take the model and the remainder, the code is as follows:
1 Private StaticString GetIP (Long ipaddr) {2 Longy = ipaddr% 256; 3 Longm = (ipaddr-y)/(256 * 256 * 256); 4 Longn = (ipaddr-256 * *256 * m-y)/(256 * 256); 5 Longx = (ipaddr-256 * *256 * m-256 * *n-y)/256; 6 returnM + "." + N + "." + x + "." +y;7}
Reference: http://blog.csdn.net/shb_derek1/article/details/8064308
Conversion of IP addresses using bit operations