Skip to content

Instantly share code, notes, and snippets.

@dumbmoron
Created April 3, 2025 22:40
Show Gist options
  • Save dumbmoron/2c9fc604dd797d2eaf44fc40ba477eda to your computer and use it in GitHub Desktop.
Save dumbmoron/2c9fc604dd797d2eaf44fc40ba477eda to your computer and use it in GitHub Desktop.
const sub = net => 2 ** (32 - net),
fits = (net, num) => num - sub(net) >= 0;
const nets = num => {
const cidr = [];
while (num) {
let net = 32;
while (fits(num, net - 1))
net--;
cidr.push(net);
num -= sub(net);
}
return cidr
}
nets(506112); // [14, 15, 16, 17, 19, 20, 21, 24]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment