Last active
August 29, 2015 14:07
-
-
Save oharsta/4f5a9f01cfebafc6d35f to your computer and use it in GitHub Desktop.
Calculate the addition of IPv4 and IPv6 prefix lengths (e.g. a IPv4 /23 and a /23 result in a /22)
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
object PrefixCalculator { | |
def sumIpv4(ipv4Prefixes: Seq[Int]) : Int = doSumIpvX(ipv4Prefixes, 32) | |
def sumIpv6(ipv6Prefixes: Seq[Int]) : Int = doSumIpvX(ipv6Prefixes, 128) | |
private def doSumIpvX(prefixes: Seq[Int], bitSize: Int) : Int = { | |
val addressSize = prefixes.map(prefix => Math.pow(2, bitSize - prefix)).sum | |
(bitSize - (Math.log(addressSize) / Math.log(2))).floor.toInt | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment