| Bytes | Lang | Time | Link |
|---|---|---|---|
| 074 | PHP | 190215T170436Z | 640KB |
| 103 | Factor | 160409T193746Z | cat |
| 092 | JavaScript ES6 | 151225T020415Z | user8165 |
| 085 | Perl | 151225T160819Z | Kenney |
| 126 | PHP | 151225T112501Z | Aurea |
| 042 | Pyth | 151225T025139Z | PurkkaKo |
| 047 | MATL | 151225T023800Z | Luis Men |
PHP, 74 bytes
<?=long2ip($i=ip2long($argv[1])&$m=ip2long($argv[2])),' ',long2ip($i|~$m);
As a standalone, input is via command line:
$ php ip.php 192.168.0.1 255.255.255.0
192.168.0.0 192.168.0.255
Or as a function, 80 bytes:
function($a,$b){return[long2ip($i=ip2long($a)&$m=ip2long($b)),long2ip($i|~$m)];}
Ungolfed
function ip( $a, $b ) {
$i = ip2long( $a ); // string IP to 32 bit int
$m = ip2long( $b ); // string netmask to 32 bit int
$n = $i & $m; // network is bitwise AND of IP and netmask
$c = $i | ~$m; // broadcast is bitwise OR of IP and inverse netmask
return [ long2ip( $n ), long2ip( $c ) ];
}
PHP has nice (albeit with long function names) built-ins to handle IPv4 dotted-string to binary and back.
Output
192.168.0.1 255.255.255.0 => 192.168.0.0 192.168.0.255
192.168.0.0 255.255.255.0 => 192.168.0.0 192.168.0.255
192.168.0.255 255.255.255.0 => 192.168.0.0 192.168.0.255
100.200.100.200 255.255.255.255 => 100.200.100.200 100.200.100.200
1.2.3.4 0.0.0.0 => 0.0.0.0 255.255.255.255
10.25.30.40 255.252.0.0 => 10.24.0.0 10.27.255.255
Factor, 103 bytes
[ [ ipv4-aton ] bi@ 2dup bitand -rot dup bit-count 32 - abs on-bits pick bitor 2nip [ ipv4-ntoa ] bi@ ]
Nice.
Ungolfed:
: mask-and-broadcast ( ip mask -- netaddr broadcast )
[ ipv4-aton ] bi@ 2dup bitand -rot dup bit-count 32 - abs on-bits pick bitor 2nip
[ ipv4-ntoa ] bi@ ;
JavaScript (ES6), 92 bytes
(a,m)=>a.split`.`.map((n,i)=>(v=m[i],m[i]=n&v|v^255,n&v),m=m.split`.`).join`.`+" "+m.join`.`
Explanation
(a,m)=>
a.split`.`
.map((n,i)=>(
v=m[i],
m[i]=n&v|v^255,
n&v
),
m=m.split`.`
).join`.`
+" "+m.join`.`
Test
var solution = (a,m)=>a.split`.`.map((n,i)=>(v=m[i],m[i]=n&v|v^255,n&v),m=m.split`.`).join`.`+" "+m.join`.`
<input type="text" id="input" value="10.25.30.40 255.252.0.0" />
<button onclick="result.textContent=solution.apply(null,input.value.split` `)">Go</button>
<pre id="result"></pre>
Perl, 90 85 bytes
includes +6 for -pF/\D/
for(0..3){push@a,$F[$_]&1*($y=$F[$_+4]);push@b,$F[$_]|~$y&255}$"='.';$_="@a @b"
Usage:
echo "192.168.0.1 255.255.255.0" | perl -pF/\\D/ file.pl
More readable:
for(0..3) {
push @a, $F[$_] & 1*($y=$F[$_+4]); # calc/add network address 'byte'
push @b, $F[$_] | ~$y & 255 # calc/add broadcast address 'byte'
}
$"='.'; # set $LIST_SEPARATOR
$_="@a @b" # set output to network address and broadcast
The -F/\D/ splits the input on non-digits and stores it in @F.
PHP, 126 bytes
With input in $n:
preg_filter(~Ð×£ÔÖÐ,~Û¤¢Â×ÛÎÖÑ×ÛÔÔÁÌÀ×ÛÑÂ×ÛÂÛÁÊÀÝÑÝÅÝÝÖÑ×Û¤ÛÒÊ¢ÙÛÎÖÖÑ×ÛÑÂÛÑ×Û¤ÛÒÊ¢ÍÊÊÙÛÎÖÖÅÝÝÖ,$n);echo"$c $b";
Hexdump:
0000000: 7072 6567 5f66 696c 7465 7228 7ed0 d7a3 preg_filter(~...
0000010: 9bd4 d6d0 9a2c 7edb 9ea4 a2c2 d7db ced6 .....,~.........
0000020: d1d7 db96 d4d4 c1cc c0d7 db9c d1c2 d7db ................
0000030: 8bc2 db96 c1ca c0dd d1dd c5dd ddd6 d1d7 ................
0000040: db9e a4db 96d2 caa2 d9db ced6 d6d1 d7db ................
0000050: 9dd1 c2db 8bd1 d7db 9ea4 db96 d2ca a283 ................
0000060: cdca cad9 81db ced6 d6c5 dddd d62c 246e .............,$n
0000070: 293b 6563 686f 2224 6320 2462 223b );echo"$c $b";
And a more readable version:
preg_filter( /* PCRE regex on input */
'/(\d+)/e', /* match all digits, execute the code for each one */
'$a[]=($1) /* push the matched value to the array $a */
.($i++>3 /* if we're at the 5th or higher digit */
?($c.=($t=$i>5?".":"").($a[$i-5]&$1)) /* append to $c bitwise AND-ed number */
.($b.=$t.($a[$i-5]|255&~$1)) /* append to $b the broadcast address */
:"")',
$n);
echo"$c $b"; /* output $c and $b */
preg_filter requires a single statement in the replacement pattern when using the e flag, so I 'append' the result of the calculations to the 5th and higher values of $a, because those are never reused.
Pyth, 44 42 bytes
LisMcb\.256Lj\..[04jb256'J.&yzKyw'+JxKt^2 32
Expects the input as an array, like ["10.25.30.40", "255.252.0.0"].
MATL, 47 bytes
This answer uses current version (4.0.0) of the language.
'%i.%i.%i.%i't32whh2:"j'\d+'XXU]tbZ&tb255Z~+hYD
Example
>> matl
> '%i.%i.%i.%i't32whh2:"j'\d+'XXU]tbZ&tb255Z~+hYD
>
> 192.168.0.1
> 255.255.255.0
192.168.0.0 192.168.0.255
Explanation
'%i.%i.%i.%i't32whh % format string: '%i.%i.%i.%i %i.%i.%i.%i'
2:" % for loop: do this twice
j'\d+'XXU % input string and parse into 4-vector with the numbers
] % end
tbZ& % compute network address
tb255Z~+ % compute broadcast address
hYD % concatenate into 8-vector and apply format string