# IP Addressing Practice Programs
## Subnetting Calculator Logic
Understanding how to write a subnetting calculator helps reinforce subnetting concepts. A subnetting calculator takes an IP address and prefix length as input and calculates the network address, broadcast address, first usable host, last usable host, total hosts, and usable hosts.
To find the network address, perform a bitwise AND operation between the IP address and the subnet mask. Every bit where both the IP address and subnet mask have a 1 results in a 1 in the network address. Where the subnet mask has a 0, the network address also has a 0 regardless of the IP address bit.
To find the broadcast address, take the network address and set all host bits to 1. This means performing a bitwise OR operation between the network address and the inverse of the subnet mask.
The first usable host address is the network address plus one. The last usable host address is the broadcast address minus one. The total number of addresses in the subnet is 2 to the power of the number of host bits. The number of usable hosts is the total number of addresses minus 2.
## Example Calculation
Given the address 192.168.10.50/26, first determine the subnet mask. A /26 subnet mask is 255.255.255.192 in decimal, or 11111111.11111111.11111111.11000000 in binary.
Convert the IP address to binary: 11000000.10101000.00001010.00110010.
Apply the AND operation with the subnet mask: 11000000.10101000.00001010.00000000, which is 192.168.10.0. This is the network address.
The inverse of the subnet mask is 00000000.00000000.00000000.00111111. Apply OR with the network address: 11000000.10101000.00001010.00111111, which is 192.168.10.63. This is the broadcast address.
The first usable host is 192.168.10.1 and the last usable host is 192.168.10.62. The subnet has 64 total addresses and 62 usable hosts.
## Binary to Decimal Conversion Practice
Converting between binary and decimal is essential for subnetting. Each octet has 8 bit positions with values 128, 64, 32, 16, 8, 4, 2, and 1 from left to right. To convert binary 10110100 to decimal, add the values of the positions where there is a 1: 128 plus 32 plus 16 plus 4 equals 180. To convert decimal 172 to binary, determine which power-of-2 values sum to 172: 128 plus 32 plus 8 plus 4 equals 172, so the binary is 10101100.
## IP Range Calculation Practice
Given a network address and prefix, practice calculating the valid host range. For 10.0.0.0/8, the valid host range is 10.0.0.1 to 10.255.255.254. For 172.16.0.0/16, the valid host range is 172.16.0.1 to 172.16.255.254. For 192.168.5.0/24, the valid host range is 192.168.5.1 to 192.168.5.254. For 192.168.5.128/25, the valid host range is 192.168.5.129 to 192.168.5.254.Back to Subject