Php converts an IP address to a broadcast address

Source: Internet
Author: User
Network broadcast address calculation method: (1) bitwise AND operation on the IP address and subnet mask to obtain the network address (2) "inverse" Operation on the subnet mask, then perform the bitwise OR operation with the network address to obtain the ip address of the broadcast address. Convert the ip address of the broadcast address privatefunctionip2broadcast ($ ip, $ mask) {$ ipSplitexplode (., $ ip); $ ma

Calculation Method of the network broadcast address: (1) bitwise and operation of the IP address and subnet mask to obtain the network address (2) "inverse" Operation of the subnet mask, then perform the bitwise OR operation with the network address to obtain the broadcast address // ip address translation broadcast address private function ip2broadcast ($ ip, $ mask) {$ ipSplit = explode ('. ', $ ip); $ ma

Network broadcast address calculation method:
(1) perform bitwise AND operations on the IP address and subnet mask to obtain the network address.
(2) Calculate the subnet mask "inverse", and then perform "bitwise OR" operation with the network address to obtain the broadcast address.

// Ip address translation broadcast address private function ip2broadcast ($ ip, $ mask) {$ ipSplit = explode ('. ', $ ip); $ maskSplit = explode ('. ', $ mask); $ broadcast = null; for ($ I = 0; $ I <4; $ I ++) {$ ipBin = sprintf ("% B ", $ ipSplit [$ I]); while (8-strlen ($ ipBin)> 0) $ ipBin = "0 ". $ ipBin; $ maskBin = sprintf ("% B", $ maskSplit [$ I]); while (8-strlen ($ maskBin)> 0) $ maskBin = "0 ". $ maskBin; $ broadcastBin = null; for ($ j = 0; $ j <8; $ j ++) {$ broadcastBin. = ( ! Intval (substr ($ maskBin, $ j, 1) | (intval (substr ($ ipBin, $ j, 1) & intval (substr ($ maskBin, $ j, 1);} if ($ I> 0) $ broadcast. = ". "; $ broadcast. = bindec ($ broadcastBin);} return $ broadcast ;}

Title: php converts IP addresses to broadcast addresses

Link to this article: http://www.maben.com.cn/archives/604.html.

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.