0

I have a task to complete in C#. I have a Subnet Name: 192.168.10.0/24

I need to find the subnet mask, which would be, in this case, 255.255.255.0.

However, I need to be able to do this in C# WITHOUT the use of the System.Net library (the system I am programming in does not have access to this library).

It seems like the process should be something like:

1) Split the Subnet Name into Number and Bits.

2) Shove the Bits into this that I have found on SO (thanks to Converting subnet mask "/" notation to Cisco 0.0.0.0 standard):

var cidr = 24; // e.g., "/24" var zeroBits = 32 - cidr; // the number of zero bits var result = uint.MaxValue; // all ones // Shift "cidr" and subtract one to create "cidr" one bits; // then move them left the number of zero bits. result &= (uint)((((ulong)0x1 << cidr) - 1) << zeroBits); // Note that the result is in host order, so we'd have to convert // like this before passing to an IPAddress constructor result = (uint)IPAddress.HostToNetworkOrder((int)result); 

However, the problem that I have is that I do not have access to the library that contains the IPAddress.HostToNetworkOrder command in the system that I am working. Also, my C# is pretty poor. Does anyone have the C# knowledge to help?

2
  • The code is now: uint bitsUInt = Convert.ToUInt32(bits); uint zeroBits = 32 - bitsUInt; uint result = uint.MaxValue; result &= ((((ulong)0x1 << bitsUInt) - 1) << zeroBits);string maskString = result.ToString(); Sadly, the interpreter says, "Operator '<<' cannot be applied to operands of type 'ulong' and 'uint'". What am I doing wrong? Commented Oct 25, 2012 at 11:09
  • C/C++ is much more elegant when this close to the bare metal. If you had a choice, that would be a better choice than C#. I'm guessing you don't have a choice here, so nice to see you found an answer, but if performance is a consideration, find a better one. Commented Jan 4, 2014 at 21:34

2 Answers 2

3

You could replace that method with the following:

static void ToNetworkByteOrder(ref uint n) { if(BitConverter.IsLittleEndian) { // need to flip it n = ( (n << 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8) | (n >> 24) ); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Hmmm. I have had to re-type the code, as the interpreter seems not to like vars and so I needed to convert everything into uints. The problem that I now have is that it says on the result &= ((((ulong)0x1 << cidr) - 1) << zeroBits); line that Operator '<<' cannot be applied to operands of type 'ulong' and 'unit'.
I don't see anything wrong with the current code. Are you are using C# 2? The var keyword should be available in VS 2008 or newer. cidr and zeroBits should be fine as int which would be their implicit type using the var keyword. result will also be fine as it will be a uint. All you should have to change is the last line, into: ToNetworkByteOrder(ref result);. You can change the method to return the result if you are more comfortable with that or read up on ref on MSDN: msdn.microsoft.com/en-us/library/14akc2c7(v=VS.71).aspx.
2

Here's a simpler way to get the mask:

int mask = -1 << (32 - cidr); 

You don't need the Net assembly to get the bytes in the right order, you can use the BitConverter class:

if (BitConverter.IsLittleEndian) { byte[] parts = BitConverter.GetBytes(mask); Array.Reverse(parts); mask = BitConverter.ToInt32(parts, 0); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.