MCPcopy Index your code
hub / github.com/qiyuangong/leetcode / ipToCIDR

Method ipToCIDR

java/751_IP_to_CIDR.java:2–13  ·  view source on GitHub ↗
(String ip, int n)

Source from the content-addressed store, hash-verified

1class Solution {
2 public List<String> ipToCIDR(String ip, int n) {
3 long start = ipToLong(ip);
4 List<String> ans = new ArrayList();
5 while (n > 0) {
6 int mask = Math.max(33 - bitLength(Long.lowestOneBit(start)),
7 33 - bitLength(n));
8 ans.add(longToIP(start) + "/" + mask);
9 start += 1 << (32 - mask);
10 n -= 1 << (32 - mask);
11 }
12 return ans;
13 }
14 private long ipToLong(String ip) {
15 long ans = 0;
16 for (String x: ip.split("\\.")) {

Callers

nothing calls this directly

Calls 5

ipToLongMethod · 0.95
bitLengthMethod · 0.95
longToIPMethod · 0.95
maxMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected