title: How to calculate end IP in the range, based on start IP and network mask length
body: <p>I have a starting IPv4 IP address <code>5.39.28.128</code> (or <code>::ffff:5.39.28.128</code>) and I have the IPv6 network mask length <code>122</code>, how can I calculate the last IP in the range?</p>
  
  <p>I believe I need to convert the start IP to binary, which I'm doing like below, I don't know where to go from there to get the end IP.</p>
  
  <pre><code>$ipNumber = ip2long('5.39.28.128');
  $ipBinary = decbin($ipNumber);
  
  echo $ipBinary; // 101001001110001110010000000
  </code></pre>
  
  <p>The reason is I'm importing the MaxMind GeoIP database in CSV format into a MySQL database (so MySQL functions can be used if needed). MaxMind no longer provide the end IP, in favour of providing the start IP and the IPv6 network mask length instead.</p>