Skip to content

Instantly share code, notes, and snippets.

@aw
Created December 26, 2017 14:35
Show Gist options
  • Save aw/11e5e59b107feecd1a5b74cdaba2a54c to your computer and use it in GitHub Desktop.
Save aw/11e5e59b107feecd1a5b74cdaba2a54c to your computer and use it in GitHub Desktop.
Convert a CIDR to Subnet Netmask address in PicoLisp
# MIT Licensed
#
#(c) 2017 Alexander Williams, Unscramble <[email protected]>
#
# IPv4 "24" -> "255.255.255.0"
[de cidr-to-netmask (Mask)
(let (Pos (- 5 (/ Mask 8))
Val (& (>> (- (- 8 (% Mask 8))) 255) 255) )
(glue "."
(let N 5
(make
(loop
(cond
((= N Pos) (link Val))
((< N Pos) (link 0))
((> N Pos) (link 255)) )
(T (=1 (dec 'N))) ]
@aw
Copy link
Author

aw commented Dec 26, 2017

Usage examples

: (cidr-to-netmask "24")
!? (/ Mask 8)
"24" -- Number expected
? 
: (cidr-to-netmask 1)   
-> "128.0.0.0"
: (cidr-to-netmask 8)
-> "255.0.0.0"
: (cidr-to-netmask 18)
-> "255.255.192.0"
: (cidr-to-netmask 24)
-> "255.255.255.0"
: (cidr-to-netmask 32)
-> "255.255.255.255"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment