How do you check whether two IP addresses are in the same subnet?
Apply the subnet mask to both addresses (bitwise AND). If the network addresses match, they are in the same subnet and can talk directly. If not, traffic must go via the default gateway.
Explain it simply
Two students are in the same hostel block if their room numbers start with the same block code. The mask decides how many leading bits count as the block code.
Example
Q1: 10.0.0.5 and 10.0.1.5
with /24: networks 10.0.0.0 and 10.0.1.0 -> different subnets
with /23: both are in 10.0.0.0/23 -> same subnet
(/23 covers 10.0.0.0 to 10.0.1.255)
Q2: 192.168.1.20 and 192.168.1.130 with /25
block size 128: .20 is in 0-127, .130 is in 128-255 -> different subnetsThe same pair of addresses can be in the same or different subnets depending on the mask, so the mask must always be given.
A host does this test before sending. If the destination is in its own subnet, it uses ARP to find the destination's MAC. If not, it sends the packet to the default gateway's MAC instead. A wrong mask on one machine is a common cause of "I can ping some devices but not others".
What to say in the interview
"AND each address with the mask and compare network parts. Same result means same subnet, direct delivery. Otherwise the packet is handed to the default gateway. The answer depends on the mask, not on the addresses alone."
Common mistakes
- Comparing only the first three octets regardless of the prefix length.