Last active
July 18, 2016 09:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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