{
  "id": "subnet",
  "version": "7ede2acc509d",
  "status": "published",
  "name": "Subnet Calculator",
  "question": "What is my subnet range?",
  "summary": "Computes an IPv4 subnet from an address and a prefix length: network and broadcast addresses, usable host range and count, subnet and wildcard masks, class, and address type.",
  "category": "logic",
  "subcategory": "networks",
  "url": "https://www.acalculator.org/logic/subnet-calculator",
  "markdown": "https://www.acalculator.org/logic/subnet-calculator.md",
  "kind": "function",
  "method": "network = address AND mask; broadcast = network OR NOT mask; hosts = 2^(32 − prefix) − 2 (2 for /31, 1 for /32).",
  "assumptions": [
    "IPv4 only. Each part of the address is a whole number from 0 to 255, written without leading zeros.",
    "The network and broadcast addresses are not usable hosts, except in a /31 (RFC 3021) and a /32.",
    "The address class is historical (classful addressing, replaced by CIDR in 1993) and shown for reference."
  ],
  "inputs": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "ip": {
        "title": "IP address",
        "description": "An IPv4 address such as 192.168.1.10, optionally with its prefix after a slash (192.168.1.10/24).",
        "type": "string",
        "maxLength": 18
      },
      "prefix": {
        "title": "Subnet mask",
        "description": "The prefix length (how many leading bits are the network part) and its subnet mask.",
        "type": "string",
        "enum": [
          "0",
          "1",
          "2",
          "3",
          "4",
          "5",
          "6",
          "7",
          "8",
          "9",
          "10",
          "11",
          "12",
          "13",
          "14",
          "15",
          "16",
          "17",
          "18",
          "19",
          "20",
          "21",
          "22",
          "23",
          "24",
          "25",
          "26",
          "27",
          "28",
          "29",
          "30",
          "31",
          "32"
        ]
      }
    }
  },
  "outputs": {
    "range": {
      "label": "Usable host range",
      "description": "The first and last addresses that can be given to hosts on this subnet.",
      "format": "text"
    },
    "hosts": {
      "label": "Usable hosts",
      "description": "How many addresses can be given to hosts: 2^(32 − prefix) − 2, except 2 for a /31 (RFC 3021) and 1 for a /32.",
      "format": "integer"
    },
    "network": {
      "label": "Network address",
      "description": "The address with every host bit set to 0.",
      "format": "text"
    },
    "broadcast": {
      "label": "Broadcast address",
      "description": "The address with every host bit set to 1. A /31 or /32 has none.",
      "format": "text"
    },
    "cidr": {
      "label": "CIDR notation",
      "description": "The network address and prefix length.",
      "format": "text"
    },
    "total": {
      "label": "Total addresses",
      "description": "Every address in the subnet, including the network and broadcast addresses: 2^(32 − prefix).",
      "format": "integer"
    },
    "mask": {
      "label": "Subnet mask",
      "description": "The prefix as a dotted mask.",
      "format": "text"
    },
    "wildcard": {
      "label": "Wildcard mask",
      "description": "The subnet mask with every bit flipped, as used in router access lists.",
      "format": "text"
    },
    "maskBinary": {
      "label": "Subnet mask in binary",
      "description": "The mask as 32 bits, in four groups of 8 separated by spaces.",
      "format": "text"
    },
    "addressClass": {
      "label": "Address class",
      "description": "The historical classful class of the address (A to E), from its first number.",
      "format": "text"
    },
    "addressType": {
      "label": "Address type",
      "description": "Public, or the special-purpose block the address is in (private, loopback, link-local, and others).",
      "format": "text"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "ip": "192.168.1.10",
      "prefix": "24"
    },
    "outputs": {
      "range": "192.168.1.1 – 192.168.1.254",
      "hosts": 254,
      "network": "192.168.1.0",
      "broadcast": "192.168.1.255",
      "cidr": "192.168.1.0/24",
      "total": 256,
      "mask": "255.255.255.0",
      "wildcard": "0.0.0.255",
      "maskBinary": "11111111 11111111 11111111 00000000",
      "addressClass": "C",
      "addressType": "Private"
    },
    "text": "192.168.1.0/24 has 254 usable hosts: 192.168.1.1 – 192.168.1.254."
  },
  "examples": [
    {
      "given": {
        "ip": "192.168.1.10",
        "prefix": "24"
      },
      "expect": {
        "range": "192.168.1.1 – 192.168.1.254",
        "hosts": 254,
        "network": "192.168.1.0",
        "broadcast": "192.168.1.255",
        "cidr": "192.168.1.0/24",
        "total": 256,
        "mask": "255.255.255.0",
        "wildcard": "0.0.0.255",
        "maskBinary": "11111111 11111111 11111111 00000000",
        "addressClass": "C",
        "addressType": "Private"
      },
      "source": "hand calculation in content.mdx (RFC 4632 prefix, RFC 950 all-zeros and all-ones host numbers, RFC 1918 private block); Python ipaddress module (ip_network, strict off)"
    },
    {
      "given": {
        "ip": "10.20.30.40/13"
      },
      "expect": {
        "range": "10.16.0.1 – 10.23.255.254",
        "hosts": 524286,
        "network": "10.16.0.0",
        "broadcast": "10.23.255.255",
        "mask": "255.248.0.0",
        "wildcard": "0.7.255.255",
        "addressClass": "A",
        "addressType": "Private"
      },
      "source": "hand calculation in content.mdx: second number 20 = 00010100, keep 5 bits → 16; 2^19 − 2 = 524,286; Python ipaddress module (ip_network, strict off)"
    },
    {
      "given": {
        "ip": "172.16.5.4",
        "prefix": "31"
      },
      "expect": {
        "range": "172.16.5.4 – 172.16.5.5",
        "hosts": 2,
        "network": "172.16.5.4",
        "total": 2
      },
      "source": "RFC 3021: both addresses of a /31 are host addresses; the hosts of 172.16.5.4/31 in the Python ipaddress module"
    },
    {
      "given": {
        "ip": "203.0.113.77",
        "prefix": "32"
      },
      "expect": {
        "range": "203.0.113.77",
        "hosts": 1,
        "cidr": "203.0.113.77/32",
        "addressType": "Documentation"
      },
      "source": "hand calculation in content.mdx: a /32 is a single host; RFC 5737 TEST-NET-3 is 203.0.113.0/24"
    },
    {
      "given": {
        "ip": "192.168.1.200",
        "prefix": "26"
      },
      "expect": {
        "range": "192.168.1.193 – 192.168.1.254",
        "hosts": 62,
        "total": 64,
        "network": "192.168.1.192",
        "mask": "255.255.255.192",
        "wildcard": "0.0.0.63"
      },
      "source": "hand calculation in content.mdx: 2^(32 − 26) = 64 addresses, 64 − 2 = 62 hosts; 200 = 11001000, keep 2 bits → 192"
    }
  ],
  "sources": [
    "RFC 4632, Classless Inter-domain Routing (CIDR): The Internet Address Assignment and Aggregation Plan. https://www.rfc-editor.org/rfc/rfc4632",
    "RFC 950, Internet Standard Subnetting Procedure (all-zeros and all-ones host numbers). https://www.rfc-editor.org/rfc/rfc950",
    "RFC 3021, Using 31-Bit Prefixes on IPv4 Point-to-Point Links. https://www.rfc-editor.org/rfc/rfc3021",
    "RFC 1918, Address Allocation for Private Internets. https://www.rfc-editor.org/rfc/rfc1918",
    "IANA IPv4 Special-Purpose Address Registry (RFC 6890). https://www.iana.org/assignments/iana-ipv4-special-registry/",
    "RFC 791, Internet Protocol, section 3.2 (address classes A, B, C). https://www.rfc-editor.org/rfc/rfc791"
  ],
  "related": [
    "raid"
  ],
  "changelog": []
}
