January 20, 2014

Converting CIDR (Classless Inter-Domain Routing) to IP address range

CIDR notation represents an IP address range using a combination of an IP address and its associated network mask. Instead of specifying the network mask in IP format (xxx.xxx.xxx.xxx), CIDR notation uses number.

The format of CIDR notation is:


Where xxx.xxx.xxx.xxx is the address space, and n is the number of left most 1 bits in the network mask. 

For example, if 'n' is 18, then the corresponding network mask is:

There are online tool, which accepts input in CIDR format and outputs the IP range for example refer: http://www.ipaddressguide.com/cidr

Given below is a program I wrote in node.js, which produce the IP range for a given CIDR input:

Note: I had to design and implement network commands to manage Windows Azure Network using Windows Azure Tool for Node.js, as a part of implementation i used below logic to convert CIDR to IP range.

var addressSpace = // The first part of cidr value xxx-xxx-xxx-xxx
var cidr = // the second part of the cidr value n
var addressSpaceRange = {start:null, end:null};
var addressSpaceAsOctects = {
  'octect0': null,
  'octect1': null,
  'octect2': null,
  'octect3': null,
var networkMaskAsoctects = {
  'octect0': 0xff,
  'octect1': 0xff,
  'octect2': 0xff,
  'octect3': 0xff,
var ipv4Pattern = new RegExp(/^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$/);
var patternMatch = addressSpace.match(this.ipv4Pattern);
for (var i = 0; i < 4; i++) {
  addressSpaceAsOctects ['octect' + i] = parseInt(patternMatch[i + 1]);
var i = Math.floor(cidr / 8);
networkMaskAsoctects['octect' + i] = (0xff << (8 - cidr % 8)) & 0xff;
for (var j = i + 1; j < 4; j++) {
  networkMaskAsoctects['octect' + j] = 0;
addressSpaceRange.start =  {
  'octect0': addressSpaceAsOctects.octect0 & networkMaskAsoctects.octect0,
  'octect1': addressSpaceAsOctects.octect1 & networkMaskAsoctects.octect1,
  'octect2': addressSpaceAsOctects.octect2 & networkMaskAsoctects.octect2,
  'octect3': addressSpaceAsOctects.octect3 & networkMaskAsoctects.octect3,
addressSpaceRange.end = {
  'octect0': addressSpaceAsOctects.octect0 | (~(networkMaskAsoctects.octect0) & 0xff),
  'octect1': addressSpaceAsOctects.octect1 | (~(networkMaskAsoctects.octect1) & 0xff),
  'octect2': addressSpaceAsOctects.octect2 | (~(networkMaskAsoctects.octect2) & 0xff),

  'octect3': addressSpaceAsOctects.octect3 | (~(networkMaskAsoctects.octect3) & 0xff),


  1. I've seen that different tools give different results when it comes to calculating from CIDR /31 or /32, because there is not such a thing like broadcast address and not only.

    The online tool you suggested is pretty good, but it accepts only 1 IP at a time and you cannot export them to CSV. In order to make a lot of CIDR conversion it's better to use http://ipconvertertools.com/

  2. @Seekpik thank you!, the tool you suggested seems pretty good..