Created
April 3, 2025 22:40
-
-
Save dumbmoron/2c9fc604dd797d2eaf44fc40ba477eda to your computer and use it in GitHub Desktop.
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
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