How IP addresses and subnet masks work together
An IPv4 address is 32 bits written as four decimal octets, like 192.168.10.50. The subnet mask tells you which bits are the network portion and which are the host portion. A /24 mask (255.255.255.0) means the first 24 bits identify the network and the last 8 bits identify the host within that network. Every device on the same subnet shares the same network bits.
Think of an IP address like a mailing address. The subnet mask defines how much of that address is the street and how much is the house number. Two houses on the same street share a network; two houses on different streets need a router to exchange mail.
CIDR notation (the slash number) tells you directly how many bits are in the network portion. /24 means 24 network bits. /28 means 28 network bits, leaving 4 bits for hosts. The more bits in the mask, the smaller the subnet.
Calculating subnets, hosts, and ranges
The number of usable hosts in a subnet is 2 to the power of the host bits, minus 2. You subtract 2 because the first address in every subnet is the network address (all host bits zero) and the last is the broadcast address (all host bits one). Neither can be assigned to a device. A /28 subnet has 4 host bits, so 2^4 minus 2 equals 14 usable hosts.
To find the network address of a subnet given an IP and mask, you AND the IP address against the mask bit by bit. For exam speed, the faster approach is to find the block size: 256 minus the interesting octet of the mask. A /26 mask has 192 (11000000) in the fourth octet, so 256 minus 192 equals 64. Subnets start at 0, 64, 128, and 192 in that octet. The IP 10.0.0.100 with a /26 mask falls in the 64 subnet (range 64 to 127), so the network address is 10.0.0.64 and the broadcast is 10.0.0.127.
The block size method works for any prefix length. /25 gives block size 128, /27 gives 32, /28 gives 16, /29 gives 8, /30 gives 4. Memorize these and subnet questions become arithmetic rather than binary conversion.
VLSM and address planning
Variable Length Subnet Masking (VLSM) lets you use different mask lengths for different subnets within the same larger network. Instead of forcing every segment to use the same block size, you size each subnet for its actual need. A point-to-point WAN link needs only two hosts, so give it a /30 (2 usable hosts) instead of wasting a /24. A large LAN segment with 100 hosts needs at least a /25 (126 usable hosts).
VLSM planning order: always subnet from largest requirement to smallest. Start with the segment needing the most hosts. Assign the smallest mask that fits, then work down. This prevents address overlap and makes efficient use of your address space.
How to choose the correct answer
Given an IP and mask, find the subnet: calculate block size (256 minus the mask octet), find which block the IP falls into, network address is the start of that block, broadcast is one before the next block.
Given a number of hosts needed, find the right mask: find the smallest power of 2 greater than hosts plus 2, the exponent is the number of host bits, subtract from 32 to get the prefix length.
Overlapping subnets: two IPs are in the same subnet if they share the same network address when ANDed with the mask. If they produce different network addresses, they are in different subnets.
For VLSM: subnet the largest requirement first, then carve the remaining space for smaller needs.