<?php
str decbin ( int $number )
where,
$number = The decimal value to be converted to binary
?>
MINIMUM INTEGER = BINARY 32 or 64bit |
-4294967294 = =1111111111111111111111111111111100000000000000000000000000000010 32bit |
-9223372036854775806 = =1000000000000000000000000000000000000000000000000000000000000010 64bit |
ed48 |
MAXIMUN INTEGER = BINARY 32 or 64bit |
4294967295 = =11111111111111111111111111111111 32bit |
9223372036854775807 = =111111111111111111111111111111111111111111111111111111111111111 64bit |
ed48 |
<?php
$nbr32_01 = 1;
$nbr32_02 = 3;
$nbr32_03 = 2147483646;
$nbr32_04 = 2147483647;
$nbr32_05 = -2147483648;
$nbr32_06 = 2147483648;
$nbr32_07 = 4294967294;
$nbr32_08 = -2;
$nbr32_09 = 4294967295;
$nbr32_10 = -1;
$arr32_01_07 = [ $nbr32_01, $nbr32_02, $nbr32_03,
$nbr32_04, $nbr32_05, $nbr32_06, $nbr32_07,
$nbr32_08, $nbr32_09, $nbr32_10 ];
foreach ($arr32_01_07 as $nbr)
{
echo 'decbin( ' . $nbr . ' ) = ' . decbin($nbr) . '<br><br>';
}
?>
<?php
$nbr64_01 = 1;
$nbr64_02 = 3;
$nbr64_03 = 9223372036854775806;
$nbr64_04 = 9223372036854775807;
$nbr64_05 = -9223372036854775806;
$nbr64_06 = -5;
$nbr64_07 = -1;
$arr64_01_07 = [ $nbr64_01, $nbr64_02, $nbr64_03,
$nbr64_04, $nbr64_05, $nbr64_06, $nbr64_07 ];
foreach ($arr64_01_07 as $nbr)
{
echo 'decbin( ' . $nbr . ' ) = ' . decbin($nbr) . '<br><br>';
}
?>
<?php
$values = array(10,
3950.5,
3.9505e3,
03,
0x5F,
"10",
"3950.5",
"3.9505e3",
"039",
"0x5F",
true,
false,
null,
);
foreach ($values as $value) {
try {
var_dump(decbin($value));
echo '<br><br>';
} catch (TypeError $exception) {
echo $exception->getMessage() . "<br><br>";
}
}
?>
<?php
if (PHP_INT_SIZE != 8)
die("skip this test is for 64bit platform only");
define("MAX_64Bit", 9223372036854775807);
define("MAX_32Bit", 2147483647);
define("MIN_64Bit", -9223372036854775807 - 1);
define("MIN_32Bit", -2147483647 - 1);
$longVals = array(
MAX_64Bit, MIN_64Bit, MAX_32Bit, MIN_32Bit,
MAX_64Bit - MAX_32Bit, MIN_64Bit - MIN_32Bit,
MAX_32Bit + 1, MIN_32Bit - 1, MAX_32Bit * 2,
(MAX_32Bit * 2) + 1, (MAX_32Bit * 2) - 1,
MAX_64Bit -1, MAX_64Bit + 1,
MIN_64Bit + 1, MIN_64Bit - 1
);
foreach ($longVals as $longVal) {
echo "Testing: $longVal<br>";
try {
var_dump(decbin($longVal));
echo '<br><br>';
} catch (TypeError $exception) {
echo $exception->getMessage() . "<br><br>";
}
}
?>
<?php
/*
if (PHP_INT_SIZE != 4)
die("skip this test is for 32bit platform only");
*/
echo "Testing decbin() : usage variations<br><br>";
//get an unset variable
$unset_var = 10;
unset ($unset_var);
// heredoc string
$heredoc = <<<EOT
abc
xyz
EOT;
// get a class
class classA
{
}
// get a resource variable
$fp = fopen(__FILE__, "r");
$inputs = array(
// int data
/*1*/ 0,
1,
12345,
-2345,
4294967295,
// largest decimal
4294967296,
// float data
/*7*/ 10.5,
-10.5,
12.3456789000e10,
12.3456789000E-10,
.5,
// null data
/*12*/ NULL,
null,
// boolean data
/*14*/ true,
false,
TRUE,
FALSE,
// empty data
/*18*/ "",
'',
array(),
// string data
/*21*/ "abcxyz",
'abcxyz',
$heredoc,
// object data
/*24*/ new classA(),
// undefined data
/*25*/ @$undefined_var,
// unset data
/*26*/ @$unset_var,
// resource variable
/*27*/ $fp
);
// loop through each element of $inputs
// to check the behaviour of decbin()
foreach($inputs as $i => $input) {
$iterator = $i + 1;
echo "<br>Iteration: $iterator<br>";
try {
var_dump(decbin($input));
echo '<br><br>';
} catch (TypeError $exception) {
echo $exception->getMessage() . "<br>";
}
}
fclose($fp);
?>
<?php
if (PHP_INT_SIZE != 8)
die("skip this test is for 64bit platform only");
echo "Testing decbin() : usage variations<br><br>";
//get an unset variable
$unset_var = 10;
unset ($unset_var);
// heredoc string
$heredoc = <<<EOT
abc
xyz
EOT;
// get a class
class classAn
{
}
// get a resource variable
$fp = fopen(__FILE__, "r");
$inputs = array(
// int data
/*1*/ 0,
1,
12345,
-2345,
18446744073709551615,
// largest decimal
18446744073709551616,
// float data
/*7*/ 10.5,
-10.5,
12.3456789000e10,
12.3456789000E-10,
.5,
// null data
/*12*/ NULL,
null,
// boolean data
/*14*/ true,
false,
TRUE,
FALSE,
// empty data
/*18*/ "",
'',
array(),
// string data
/*21*/ "abcxyz",
'abcxyz',
$heredoc,
// object data
/*24*/ new classAn(),
// undefined data
/*25*/ @$undefined_var,
// unset data
/*26*/ @$unset_var,
// resource variable
/*27*/ $fp
);
// loop through each element of $inputs
// to check the behaviour of decbin()
foreach($inputs as $i => $input) {
$iterator = $i + 1;
echo "<br>Iteration: $iterator<br>";
try {
var_dump(decbin($input));
echo '<br>';
} catch (TypeError $exception) {
echo $exception->getMessage() . "<br>";
}
}
fclose($fp);
?>
<?php
$nbr07_e = 1498;
echo 'decbin( ' . $nbr07_e. ' ) = ' . decbin($nbr07_e);
?>