public static void Main()
string ipAddressString = "173.160.21.227";
string subnetMaskCIDR = "/30";
string networkAddress = GetNetworkAddress(ipAddressString, subnetMaskCIDR);
if (networkAddress != null)
Console.WriteLine($"IP Address: {ipAddressString}");
Console.WriteLine($"Subnet Mask (CIDR): {subnetMaskCIDR}");
Console.WriteLine($"Network Address: {networkAddress}");
Console.WriteLine("Invalid IP address format.");
static string GetNetworkAddress(string ipAddressString, string subnetMaskCIDR)
IPAddress ipAddress = IPAddress.Parse(ipAddressString);
int prefixLength = int.Parse(subnetMaskCIDR.Substring(1));
byte[] subnetMaskBytes = ipAddress.GetAddressBytes();
for (int i = prefixLength; i < subnetMaskBytes.Length * 8; i++)
subnetMaskBytes[byteIndex] &= (byte)~(1 << (7 - bitOffset));
IPAddress subnetMask = new IPAddress(subnetMaskBytes);
byte[] ipAddressBytes = ipAddress.GetAddressBytes();
byte[] networkBytes = new byte[ipAddressBytes.Length];
for (int i = 0; i < ipAddressBytes.Length; i++)
networkBytes[i] = (byte)(ipAddressBytes[i] & subnetMaskBytes[i]);
IPAddress networkAddress = new IPAddress(networkBytes);
return networkAddress.ToString();