public static void Main()
System.Net.IPAddress someIP = System.Net.IPAddress.Parse("192.168.1.23");
System.Net.IPAddress someMASK = System.Net.IPAddress.Parse("255.255.255.128");
long ipL = IPtoLong(someIP);
long maskL = IPtoLong(someMASK);
long oneBit = 0x80000000L;
for (int x = 31; x >= 0; x += -1)
if ((maskL & oneBit) == oneBit)
string answer = LongToIp(ipL & maskL) + "/" + CIDR.ToString();
Console.Out.WriteLine("woah woah we woah " + answer);
public static long IPtoLong(System.Net.IPAddress theIP)
byte[] IPb = theIP.GetAddressBytes();
for (int x = 0; x <= 3; x++) {
addr |= (System.Convert.ToInt64(IPb[x]) << (3 - x) * 8);
public static string LongToIp(long theIP)
byte[] IPb = new byte[4];
long mask8 = MaskFromCidr(8);
for (var x = 0; x <= 3; x++)
IPb[x] = System.Convert.ToByte((theIP & mask8) >> ((3 - x) * 8));
addr += IPb[x].ToString() + ".";
return addr.TrimEnd('.');
private static long MaskFromCidr(int CIDR)
return System.Convert.ToInt64(Math.Pow(2, ((32 - CIDR))) - 1) ^ 4294967295L;