Lesson 3 of 15

Subnet Masks & CIDR

Subnet Masks & CIDR

A subnet mask determines which portion of an IP address identifies the network and which identifies the host. CIDR (Classless Inter-Domain Routing) notation uses a slash followed by the number of network bits.

Subnet Mask Basics

IP:      192.168.1.100
Mask:    255.255.255.0   (/24)
Network: 192.168.1.0
Host:    0.0.0.100

The network address is obtained by ANDing the IP with the mask. The broadcast address has all host bits set to 1.

CIDR Notation

CIDRSubnet MaskHosts
/8255.0.0.016,777,214
/16255.255.0.065,534
/24255.255.255.0254
/28255.255.255.24014
/32255.255.255.2551

Number of usable hosts = 2^(32 - prefix) - 2 (excluding network and broadcast addresses).

Your Task

Implement subnetInfo(cidr) that takes a CIDR string (e.g., "192.168.1.100/24") and returns an object with:

  • networkAddress: the network address
  • broadcastAddress: the broadcast address
  • subnetMask: dotted-decimal mask
  • usableHosts: number of usable host addresses
Node.js loading...
Loading...
Click "Run" to execute your code.