Calculate Broadcast Address

by Jun 3, 2013

If you know the IP address and subnet mask, you can take these and calculate the broadcast address. Here's a function does it for you. Simply submit IP address and subnet mask, and receive the broadcast address:

function Get-BroadcastAddress
{
   
    param
    (
    [Parameter(Mandatory=$true)]
    $IPAddress,
    $SubnetMask='255.255.255.0'
    )

    filter Convert-IP2Decimal
    {
        ([IPAddress][String]([IPAddress]$_)).Address
    }


    filter Convert-Decimal2IP
    {
    ([System.Net.IPAddress]$_).IPAddressToString 
    }


    [UInt32]$ip = $IPAddress | Convert-IP2Decimal
    [UInt32]$subnet = $SubnetMask | Convert-IP2Decimal
    [UInt32]$broadcast = $ip -band $subnet 
    $broadcast -bor -bnot $subnet | Convert-Decimal2IP
}

This function demonstrates how you calculate network information by bitwise manipulation. The broadcast address, for example, is calculated by using the bits from the subnet and setting all remaining bits to "1".

Twitter This Tip! ReTweet this Tip!