GetBitMaskLength() public static method

Counts the number of leading 1's in a bitmask. Returns null if value is invalid as a bitmask.
public static GetBitMaskLength ( byte bytes ) : int?
bytes byte
return int?
        /// <summary>
        /// Takes a subnetmask (eg, "255.255.254.0") and returns the CIDR bit length of that
        /// address. Throws an exception if the passed address is not valid as a subnet mask.
        /// </summary>
        /// <param name="subnetMask">The subnet mask to use</param>
        /// <returns></returns>
        public static int SubnetMaskLength(IPAddress subnetMask)
        {
            var length = Bits.GetBitMaskLength(subnetMask.GetAddressBytes());

            if (length == null)
            {
                throw new ArgumentException("Not a valid subnet mask", "subnetMask");
            }
            return(length.Value);
        }