Finding the first and last address of a block of IP addresses with subnet mask

Solution 1:

/26 means 26 bits for the subnet. At 8 bits per byte you get 24 for the first three bytes, and two for the last. It means your last byte will be of the form (nnhh hhhh), where n is a subnet bit and h a host bit. 16 is indeed (0001 0000), so you get (00xx xxxx).

Thus the network address is (00|00 0000) -> .0, the broadcast is (00|11 1111) -> .63, and the first and last useable addresses are .1 and .62.

Solution 2:

IPv4 addresses are 32-bit unsigned INTs.

Since both the address and netmask can be expressed as a 32-bit unsigned INT, and they are intimately related, this is easy:

first = (addr && netmask)
last = (addr && netmask) + !netmask