From 07993c05084307debd88b4a0935a5a7c778afd40 Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Sun, 1 Jun 2014 18:23:48 +0200 Subject: [PATCH] Fixed special cases when netmask is 0 or 32 --- netcalc.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/netcalc.php b/netcalc.php index 524c4c3..d605944 100644 --- a/netcalc.php +++ b/netcalc.php @@ -56,8 +56,22 @@ function print_netmask($number) } unset($i); - // Put it together in a binary string - $binary = "$ones$zeros"; + // Special cases when netmask is 0 or 32 + if ($mask == 0) + { + // If the network mask is 0, then just put 32 zeros in the string + $binary = "00000000000000000000000000000000"; + } + elseif ($mask == 32) + { + // If the network mask is 32, then just put 32 ones in the string + $binary = "11111111111111111111111111111111"; + } + else + { + // Else put the ones and zeros together in a binary string + $binary = "$ones$zeros"; + } // Make it dotted binary format $dotted_binary = substr_replace($binary, ".", 8, 0); @@ -85,6 +99,8 @@ function print_values($value) $base=32-$value; $netsize=pow(2, $base); $hosts=$netsize-2; + + // We don't want negative values if ($hosts < 0) { $hosts = 0;