Canonicalize CIDR: Difference between revisions

J
m (→‎{{header|C sharp}}: Regularize header markup to recommended on category page)
(J)
Line 514:
184.232.184/18 -> invalid CIDR string
</pre>
 
=={{header|J}}==
 
Implementation:
 
<lang J>cidr=: {{
'a e'=. 0 ".each (y rplc'. ')-.&;:'/'
('/',":e),~rplc&' .'":_8#.\32{.e{.,(8#2)#:a
}}</lang>
 
In other words, convert address part to bits, truncate to the network address, extend back to 32 bits and repack into the ascii representation.
 
Task examples:
 
<lang J> cidr '87.70.141.1/22'
87.70.140.0/22
cidr '36.18.154.103/12'
36.16.0.0/12
cidr '62.62.197.11/29'
62.62.197.8/29
cidr '67.137.119.181/4'
64.0.0.0/4
cidr '161.214.74.21/24'
161.214.74.0/24
cidr '184.232.176.184/18'
184.232.128.0/18</lang>
 
=={{header|Java}}==
6,962

edits