## Saturday, June 01, 2019

### How To Quickly Tell The First And Last IP In a CIDR Range

This post is part of the series of posts on Tips to Quickly Perform Binary and CIDR Operations. It is borne out of my experience working on ip-num, a typescript library for working with ASN, IPv4, and IPv6 numbers.

In this post I would show the steps I have come to use to be able to determine the start IP address depicted by a CIDR notation.

### How to quickly tell the first and last ip in a CIDR range

Task: How to tell the first and last IP number in a CIDR range. Ideally the format of a CIDR range dictates that the portion of the notation that depicts the IP number should be the first number in the range, but often times, you cannot be sure that this is being adhered to when you encounter CIDR notations in the wild, so it is useful to be able to look at a CIDR notation, and tell which are the first and last IP addresses and whether the IP portion of the notation is indeed the first IP in the range.

Step to answer: Specialised to IPv4
• Have /8, /16 and /24 as prefix bit boundaries
• If the prefix is any of the prefix boundary, then the starting octet will be zero and will always be in the 2nd, 3rd and 4th octet for /8, /16, /24 respectively. If the prefix under consideration is not the bit boundary, then continue with the following steps
• Pick the bit boundary prefix which is least smaller than the prefix of the CIDR notation you are reading. So:
• if you have /26, pick /24
• If you have /23, pick /16
• If you have /19, pick /16
• If you have /12, pick /8
• If you have a prefix less than 8, pick /8 then follow the steps for prefix less than /8 below
• Subtract the prefix in your CIDR notation, from the bit boundary prefix you picked above. So If you have a 10.3.4.5/19 do 19 - 16 = 3
• Subtract the number above from 8. In this case that would be 8 - 3 = 5
• Find 2 raised to the power of the subtraction. For above that will be 25 = 32
• This means the starting octet of the first IP number must be a multiple of the value calculated in previous step. For example, in previous step, the calculated value is 32, hence the starting octet must be a multiple of 32 - 32, 64, 96 etc
• Pick which of the four octets would contain the starting octet based on the bit boundary prefix picked
• If you pick /24, the 3rd octet will be fixed, starting will be on the 4th octet: fixed.fixed.fixed.start
• If you pick /16, the 2nd octet will be fixed, starting will be on the 3rd octet:
fixed.fixed.start.x
• If you pick /8 the 1st octet will be fixed, starting will be on the 2nd octet:
fixed.start.x.x

Example:
What are the first and last IP number in 197.3.10.30/26
• /24 is the bit boundary prefix
• 26 - 24 = 2
• 8 - 2 = 6
• 26 = 64
• The octet in the starting prefix will be a multiple of 64
• /24 was picked hence the 3rd octet will be fixed and it is the 4th octet will be multiples of 64
• 197.3.10.0 is the start of the range. That is: 197.3.10.0 - 197.3.10.63
• Correct notation is 197.3.10.0/26 and 197.3.10.30/26 is incorrect. 197.3.10.30 is an ip within the 197.3.10.0/26 range, that is within the 197.3.10.0 - 197.3.10.63 range

What are the first and last IP number in 197.3.10.64/26
• /24 is the bit boundary prefix
• 26 - 24 = 2
• 8 - 2 = 6
• 26 = 64
• The octet in the starting prefix will be a multiple of 64
• /24 was picked hence the 3rd octet will be fixed and it is the 4th octet that will be multiples of 64
• 197.3.10.64 is the start of the range. That is: 197.3.10.64 - 197.3.10.127
• 197.3.10.64/26 is a correct notation

What are the first and last IP number in 197.13.23.64/19
• /16 is the bit boundary prefix
• 19 - 16 = 3
• 8 - 3 = 5
• 25 = 32
• The octet in the starting prefix will be multiple of 32
• /16 was picked hence the 2nd octet will be fixed and it is the 3rd octet that will be multiples of 32
• 197.13.0.0 is the start of the range. That is: 197.13.0.0 - 197.13.31.255
• Correct notation is 197.13.0.0/19 and 197.13.23.64/19 is incorrect. 197.13.23.64 is an ip within the 197.13.0.0/19 range, that is within the 197.13.0.0 - 197.13.31.255 range

What are the first and last IP number in 197.13.32.64/19
• /16 is the bit boundary prefix
• 19 - 16 = 3
• 8 - 3 = 5
• 25 = 32
• The octet in the starting prefix will be a multiple of 32
• /16 was picked hence the 2nd octet will be fixed and it is the 3rd octet that will be multiples of 32
• 197.13.32.64 is the start of the range. That is: 197.13.32.64 - 197.13.63.255
• 197.13.32.64/19 is a correct notation

What are the first and last IP number in 203.89.12.222/15
• /8 is the bit boundary prefix
• 15 - 8 = 7
• 8 - 7 = 1
• 21 = 2
• The octet in the starting prefix will be a multiple of 2
• /8 was picked hence the 1st octet will be fixed and it is the 2nd octet that will be multiples of 2
• 203.88.0.0 is the start of the range. That is 203.88.0.0 - 203.89.255.255
• Correct notation is 203.88.0.0/15 and 203.89.12.222/15 is incorrect. 203.89.12.222 is an ip within the 203.88.0.0/15 range, that is within the 203.88.0.0 - 203.89.255.255 range

How to quickly tell the first and last ip in a CIDR range when CIDR is less than /8

The steps to calculate the first and last IP number in a CIDR notation is slightly different if the prefix is less than /8. That is /7 to /1. The following steps should be followed:
• Subtract the prefix in your CIDR notation, from /8. So If you have a 10.3.4.5/3 do 8 - 3 = 5
• Calculate 2 raised to the power of the subtraction. For above that will be 25 = 32
• This means the starting octet of the first IP number must be a multiple of the value calculated in previous step. For example, in previous step, the calculated value is 32, hence the starting octet must be a multiple of 32 - 32, 64, 96 etc
• Pick which of the four octets would contain the starting octet based on the bit boundary prefix picked. For /8 there is no fixed octet and the first octet will be the one that contains value in the multiple of the amount calculated in previous steps.
Example:
What are the first and last IP number in 197.3.10.30/3
• /8 is the bit boundary prefix since /3 is less than /8
• 8 - 3 = 5
• 25= 32
• The octet in the starting prefix will be a multiple of 32
• The prefix /5 is less than /8 hence no fixed octet and the first octet would contain values that are multiples of 32
• 192.0.0.0 is the start of the range. That is 192.0.0.0 - 223.255.255.255
• Correct notation is 192.0.0.0/3 and 197.3.10.30/3 is incorrect. 197.3.10.30 is an ip within the 192.0.0.0/3 range, that is within the 192.0.0.0 - 223.255.255.255 range

Previous post: How To Tell Which CIDR Notation is Bigger
Next post: How many smaller IP ranges can be gotten from a bigger IP range