using System.Diagnostics;
public static class SubnetMask
public static readonly System.Net.IPAddress ClassA = System.Net.IPAddress.Parse("255.0.0.0");
public static readonly System.Net.IPAddress ClassB = System.Net.IPAddress.Parse("255.255.0.0");
public static readonly System.Net.IPAddress ClassC = System.Net.IPAddress.Parse("255.255.255.0");
public static System.Net.IPAddress CreateByHostBitLength(int hostpartLength)
int hostPartLength = hostpartLength;
int netPartLength = 32 - hostPartLength;
throw new ArgumentException("Number of hosts is to large for IPv4");
Byte[] binaryMask = new byte[4];
for (int i = 0; i < 4; i++)
if (i * 8 + 8 <= netPartLength)
binaryMask[i] = (byte)255;
else if (i * 8 > netPartLength)
int oneLength = netPartLength - i * 8;
String.Empty.PadLeft(oneLength, '1').PadRight(8, '0');
binaryMask[i] = Convert.ToByte(binaryDigit, 2);
return new System.Net.IPAddress(binaryMask);
public static System.Net.IPAddress CreateByNetBitLength(int netpartLength)
int hostPartLength = 32 - netpartLength;
return CreateByHostBitLength(hostPartLength);
public static System.Net.IPAddress CreateByHostNumber(int numberOfHosts)
int maxNumber = numberOfHosts + 1;
string b = Convert.ToString(maxNumber, 2);
return CreateByHostBitLength(b.Length);
public static class Extensions
public static int GetFirstAbsIntFast(this string intStr)
char[] n = intStr.ToCharArray();
for (int i = 0; i < n.Length; i++)
if (n[i] >= 48 && n[i] <= 57)
int z = sum * 10 + (n[i] - 48);
public static string GetIPv4NetworkMaskByPrefix(this string subnet)
int idxForwardSlash = subnet.IndexOf('/');
if (string.IsNullOrEmpty(subnet) && idxForwardSlash > -1)
string CIDRIP = subnet.Substring(idxForwardSlash+1);
numCIDRIP = CIDRIP.GetFirstAbsIntFast();
if (numCIDRIP < 1 || numCIDRIP > 32) return string.Empty;
case 1: return "128.0.0.0";
case 2: return "192.0.0.0";
case 3: return "224.0.0.0";
case 4: return "240.0.0.0";
case 5: return "248.0.0.0";
case 6: return "252.0.0.0";
case 7: return "254.0.0.0";
case 8: return "255.0.0.0";
case 9: return "255.128.0.0";
case 10: return "255.192.0.0";
case 11: return "255.224.0.0";
case 12: return "255.240.0.0";
case 13: return "255.248.0.0";
case 14: return "255.252.0.0";
case 15: return "255.254.0.0";
case 16: return "255.255.0.0";
case 17: return "255.255.128.0";
case 18: return "255.255.192.0";
case 19: return "255.255.224.0";
case 20: return "255.255.240.0";
case 21: return "255.255.248.0";
case 22: return "255.255.252.0";
case 23: return "255.255.254.0";
case 24: return "255.255.255.0";
case 25: return "255.255.255.128";
case 26: return "255.255.255.192";
case 27: return "255.255.255.224";
case 28: return "255.255.255.240";
case 29: return "255.255.255.248";
case 30: return "255.255.255.252";
case 31: return "255.255.255.254";
case 32: return "255.255.255.255";
public static string GetIPv4SubnetMask(this string subnet)
int idxForwardSlash = subnet.IndexOf('/');
if (string.IsNullOrEmpty(subnet) && idxForwardSlash > -1)
string CIDRIP = subnet.Substring(idxForwardSlash+1);
numCIDRIP = CIDRIP.GetFirstAbsIntFast();
if (numCIDRIP < 1 || numCIDRIP > 32) return string.Empty;
calSubNet = 32 - numCIDRIP;
long mask = (0xffffffffL << calSubNet & 0xffffffffL);
mask = System.Net.IPAddress.HostToNetworkOrder((int)mask);
return new System.Net.IPAddress((UInt32)mask).ToString();
public static string getSubnetAddressFromIPv4NetMask(this string netMask)
int idxForwardSlash = netMask.IndexOf('/');
if (string.IsNullOrEmpty(netMask) && idxForwardSlash > -1)
string CIDRIP = netMask.Substring(idxForwardSlash + 1);
numCIDRIP = CIDRIP.GetFirstAbsIntFast();
if (numCIDRIP < 1 || numCIDRIP > 32) return string.Empty;
calSubNet = 32 - numCIDRIP;
string subNetMask = string.Empty;
if (calSubNet >= 0 && calSubNet <= 8)
for (int ipower = 0; ipower < calSubNet; ipower++)
double finalSubnet = 255 - result;
subNetMask = "255.255.255." + Convert.ToString(finalSubnet);
else if (calSubNet > 8 && calSubNet <= 16)
int secOctet = 16 - calSubNet;
for (int ipower = 0; ipower < secOctet; 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++)
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++)
double finalSubnet = 255 - result;
subNetMask = Convert.ToString(finalSubnet) + ".0.0.0";
public static System.Net.IPAddress CreateBySubNetBitLength(this string netMask)
int idxForwardSlash = netMask.IndexOf('/');
if (string.IsNullOrEmpty(netMask) && idxForwardSlash > -1)
return System.Net.IPAddress.None;
string CIDRIP = netMask.Substring(idxForwardSlash + 1);
numCIDRIP = CIDRIP.GetFirstAbsIntFast();
if (numCIDRIP < 1 || numCIDRIP > 32) return System.Net.IPAddress.None;
int calSubNet = 32 - numCIDRIP;
return SubnetMask.CreateByNetBitLength(calSubNet);
public static void Main()
string subnetprefix = "/25";
Stopwatch sw = new Stopwatch();
Console.WriteLine("Using getSubnetAddressFromIPNetMask");
Console.WriteLine(subnetprefix.getSubnetAddressFromIPv4NetMask());
Console.WriteLine(sw.ElapsedTicks.ToString("#,0") + " ticks");
Console.WriteLine("Using compact GetSubnetMask");
Console.WriteLine(subnetprefix.GetIPv4SubnetMask());
Console.WriteLine(sw.ElapsedTicks.ToString("#,0") + " ticks");
Console.WriteLine("Using compact getNetworkMaskByPrefix");
Console.WriteLine(subnetprefix.GetIPv4NetworkMaskByPrefix());
Console.WriteLine(sw.ElapsedTicks.ToString("#,0") + " ticks");
Console.WriteLine("Using Microsoft's recommended imp - CreateBySubNetBitLength");
Console.WriteLine(subnetprefix.GetIPv4NetworkMaskByPrefix());
Console.WriteLine(sw.ElapsedTicks.ToString("#,0") + " ticks");