Introduction
This article will help you to calculate subnet mask base on NetMask and also BroadCast and Network Address
Background
The Subnet is important part in this Topic BroadCast and Network address was already present before.Based on the Netmask it will calculate the Subnet Mask and display the result.
Subnet Mask Calculation
Based on your Netmask as input it will provide the subnet mask
public string getSubnetAddressFromIPNetMask(string netMask)
{
string subNetMask = string.Empty;
int calSubNet = 0;
double result = 0;
if (!string.IsNullOrEmpty(netMask))
{
calSubNet = 32 - Convert.ToInt32(netMask);
if (calSubNet >= 0 && calSubNet <= 8)
{
for (int ipower = 0; ipower < calSubNet; ipower++)
{
result += Math.Pow(2, ipower);
}
double finalSubnet = 255 - result;
subNetMask = "255.255.255." + Convert.ToString(finalSubnet);
}
else if (calSubNet >8 && calSubNet<=16)
{
int secOctet = 16 - calSubNet;
secOctet = 8-secOctet ;
for (int ipower = 0; ipower < secOctet; ipower++)
{
result += Math.Pow(2, ipower);
}
double finalSubnet = 255 - result;
subNetMask = "255.255." + Convert.ToString(finalSubnet)+".0";
}
else if (calSubNet > 16 && calSubNet <= 24)
{
int thirdOctet = 24 - calSubNet;
thirdOctet = 8-thirdOctet ;
for (int ipower = 0; ipower < thirdOctet; ipower++)
{
result += Math.Pow(2, ipower);
}
double finalSubnet = 255 - result;
subNetMask = "255." + Convert.ToString(finalSubnet) + ".0.0";
}
else if (calSubNet > 24 && calSubNet <= 32)
{
int fourthOctet = 32 - calSubNet;
fourthOctet = 8-fourthOctet ;
for (int ipower = 0; ipower < fourthOctet; ipower++)
{
result += Math.Pow(2, ipower);
}
double finalSubnet = 255 - result;
subNetMask = Convert.ToString(finalSubnet) + ".0.0.0";
}
}
return subNetMask;
}
History
Code basline