Working out 172.16.5.4/20 by hand
Subnetting looks like a trick until you do one in binary, and then it is only bookkeeping. Take the address 172.16.5.4 with a prefix of /20. The /20 says that the first 20 bits of the address are the network and the last 12 are the device. Because 20 is not a multiple of 8, the boundary falls in the middle of the third number, which is exactly the case that trips people up.
Write the mask first. Twenty ones followed by twelve zeros, grouped in eights, is 11111111.11111111.11110000.00000000. The first two groups are 255. The third group, 11110000, is 128 + 64 + 32 + 16, which is 240. The last is 0. So the subnet mask is 255.255.240.0, and the wildcard mask, every bit flipped, is 0.0.15.255.
Now apply the mask to the address. The first two numbers, 172 and 16, pass through unchanged because the mask there is all ones, and the last number becomes 0 because the mask there is all zeros. Only the third number needs work: 5 in binary is 00000101, and keeping just the top four bits (the 1111 part of 11110000) leaves 00000000, which is 0. So the network address is 172.16.0.0.
For the broadcast address, set every device bit to one instead. In the third number the low four bits become 1111, giving 00001111, which is 15, and the last number becomes 255. The broadcast address is 172.16.15.255. The first usable host is one more than the network, 172.16.0.1, and the last is one less than the broadcast, 172.16.15.254. Twelve device bits give 2 to the 12th, 4,096 addresses, and taking away the network and broadcast leaves 4,094 hosts. Type 172.16.5.4/20 into the box above and every one of those numbers appears.
A shortcut falls out of this. The size of a subnet in the octet where the boundary falls is 256 minus the mask value there: 256 minus 240 is 16. So /20 networks in the third octet start at 0, 16, 32, 48 and so on, and 5 lands in the block from 0 to 15. That is how network engineers do this in their heads.
There are limits worth knowing. The usable-host count assumes an ordinary LAN. A /31 has no network or broadcast address under RFC 3021, so both of its addresses are usable, and a /32 is a single host; the calculator follows those rules. Cloud providers often reserve more: AWS keeps five addresses in every subnet, Azure keeps five, and Google Cloud keeps four, so the number of addresses you can actually assign there is lower than the textbook answer. The "old class" line is shown only because exam questions still ask about it. Classes stopped deciding network sizes when CIDR arrived in 1993.