Skip to content

Instantly share code, notes, and snippets.

@stbuehler
Last active July 18, 2016 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stbuehler/78e2fa07b4bf70b914a85e2c2b6ee55d to your computer and use it in GitHub Desktop.
Save stbuehler/78e2fa07b4bf70b914a85e2c2b6ee55d to your computer and use it in GitHub Desktop.
align port range to 2^n start and 2^n-1 size for smallest n
#!/bin/bash
# align port range to 2^n start and 2^n-1 size for smallest n
# call: rangealign.sh a-b
a=${1%-*}
b=${1##*-}
dist=$((b-a))
n=2
while [ $dist -ge $((2**n)) ]; do
((++n));
done
n2=$((2**n))
start=$((a - a % (2**n)))
while [ $((start + 2**n - 1)) -lt $b ]; do
# echo "$dist $n $((2**n)) $start-$(($start + 2**n - 1)) ($a-$b)"
((++n))
start=$((a - a % (2**n)))
done
# echo "$dist $n $((2**n)) $start-$(($start + 2**n - 1)) ($a-$b)"
echo "$start-$(($start + 2**n - 1))"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment