Skip to content

Instantly share code, notes, and snippets.

@krsnvijay
Last active November 11, 2022 20:55
Show Gist options
  • Save krsnvijay/9be04f9f6cf18ea87d8292aa334c007e to your computer and use it in GitHub Desktop.
Save krsnvijay/9be04f9f6cf18ea87d8292aa334c007e to your computer and use it in GitHub Desktop.

Problem - check if two ips are of same subnet

  • ip1: 192.168.10.10/23
  • ip2: 192.168.11.10/23

Summary of steps

  • Find the subnet_ip, then find the num_hosts in that sub network
  • using num_hosts in that subnet find out the start_address, last_address
  • if the second ip is in that range then they are of same subnet otherwise not

Get ip,subnet_mask in binary

subnet_mask to binary

first 23 bits set to 1

  • subnet_mask_in_bin: 11111111.11111111.11111110.00000000
  • subnet_mask_in_dec: 255.255.254.0
  • num_bits_in_host: 9 (num of 0s in the last part of the mask after 1)
  • num_bits_in_network: 7 (num of 1s in the octet that is not all 0s, in this case 3rd octet)

ip1 network, host portion calculation

convert ip1 to binary

  • ip1_in_dec: 192.168.10.10
  • ip1_in_bin: 11000000.10101000.00001010.00001010

subnet_ip calculation - apply subnet_mask to ip1

if there's a 1 in subnet mask move the bit in ip1 down

octets:             --  1 --.--  2 --.--  3 --.--  4 --
ip1_in_bin:         11000000.10101000.00001010.00001010
subnet_mask_in_bin: 11111111.11111111.11111110.00000000
                    ------------------------------------
subnet_in_bin:      11000000.10101000.00001010.00000000
subnet_in_dec:      192.168.10.0

find total subnets from subnet mask

  • basically choose an octet of the subnet mask that is all 0s
  • consider only the 3rd octet of the subnet mask as it is the one determining the subnet
  • find number of 1's in that octet (num_bits_in_network)
  • num_subnets = 2^num_bits_in_network
  • num_hosts_per_subnet = 2^num_bits_in_host - 2

for ip1

  • from subnet_mask_in_bin 3rd octet is 11111110
  • num_ones = 7
  • num_subnets = 2^7 = 128
  • num_hosts_per_subnet = 2^9 - 2 = 510
  • so there are 128 subnets with each subnet having 510 hosts

find ip address range for subnets

  • starting address for subnet (network_in_bin only consider the network portion)
  subnet_in_bin:     11000000.10101000.00001010.00000000
  subnet_in_dec:     192.168.10.0
  last_address:     network_in_dec + 510 (`num_hosts_per_subnet`)
                    192.168.10.0 + 510 => 192.168.11.254 
  first_address:     192.168.10.1
  • Note:
    • 192.168.10.0 is not a usable ip address as its reserved for subnet address
    • 192.168.11.255 is not a usable ip address as its reserved for subnet address
    • basically if the last portion of ip is 0 or 255 then it can't be used by the host, which is why we subtract 2 in num_hosts_per_subnet calculation

Summary of steps

  • We found out that from ip1: 192.168.10.10/23
    • the subnet ip 192.168.10.0 has 510 hosts
      • start_address: 192.168.10.1
      • last_address: 192.168.11.254
  • as ip2: 192.168.11.10/23 is inside the range of start_address and last_address we can say that it belongs to the same network

Resources that can help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment