Calculating the number of bits in a Subnet Mask in C#

后端 未结 5 662
借酒劲吻你
借酒劲吻你 2021-01-14 06:47

I have a task to complete in C#. I have a Subnet Mask: 255.255.128.0.

I need to find the number of bits in the Subnet Mask, which would be, in this case, 17.

5条回答
  •  北海茫月
    2021-01-14 07:30

    A complete sample:

    public int CountBit(string mask)
            {
    
                int ones=0;
                Array.ForEach(mask.Split('.'),(s)=>Array.ForEach(Convert.ToString(int.Parse(s),2).Where(c=>c=='1').ToArray(),(k)=>ones++));
              return ones
    
            }
    

提交回复
热议问题