title: Explanation of convertor of cidr to netmask in linux shell netmask2cdir and cdir2netmask
body: <p>I found the following shell functions from this <a href="https://forum.openwrt.org/viewtopic.php?pid=220781#p220781" rel="noreferrer">topic</a></p>
  
  <pre><code>mask2cdr ()
  {
     # Assumes there's no "255." after a non-255 byte in the mask
     local x=${1##*255.}
     set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*}
     x=${1%%$3*}
     echo $(( $2 + (${#x}/4) ))
  }
  
  
  cdr2mask ()
  {
     # Number of args to shift, 255..255, first non-255 byte, zeroes
     set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 &lt;&lt; (8 - ($1 % 8))) &amp; 255 )) 0 0 0
     [ $1 -gt 1 ] &amp;&amp; shift $1 || shift
     echo ${1-0}.${2-0}.${3-0}.${4-0}
  }
  </code></pre>
  
  <p>Could you explain in details how these functions convert cidr to netmask and the netmask to cidr? Specifically, the calls to <code>set</code>, the parameter expansion <code>${#…}</code>, and the arithmetic expansion <code>$((…))</code> are quite overwhelming.</p>