Current Location: Home> Latest Articles> Use bindec() to handle the difference between positive and negative numbers

Use bindec() to handle the difference between positive and negative numbers

M66 2025-06-02

In PHP, the bindec() function is used to convert binary strings to decimal numbers. Its syntax is very simple:

 int bindec ( string $binary_string )

Where $binary_string is a string containing only 0 and 1 , representing a binary number.


Positive number processing

When the input binary string represents a positive number, bindec() will directly convert it to the corresponding decimal number. For example:

 <?php
echo bindec("1010");  // Output 10
echo "\n";
echo bindec("1111");  // Output 15
?>

Here the binary strings "1010" and "1111" are converted to decimal 10 and 15 respectively. This is the most common usage of bindec() , and the results are exactly as expected.


Negative number processing

Unlike the representation of signed integers, the bindec() function does not automatically identify whether a binary string represents a negative number. It always converts the input into decimal as an unsigned binary number. Therefore, if you pass in a complement (binary form) string representing a negative number directly, the result will be a large positive integer.

For example:

 <?php
echo bindec("11111111");  // Output 255,Instead -1
echo "\n";
echo bindec("10000000");  // Output 128,Instead -128
?>

Here, "11111111" represents -1 in an 8-bit signed integer, but bindec() directly sees it as an unsigned 255 . Similarly, "10000000" is -128 in 8-bit complement, but the function returns 128 .


How to correctly handle negative numbers?

If you need to process negative numbers based on binary strings, you must implement the judgment and conversion of complement code by yourself. A simple way is:

  1. Determine the highest bit (the highest bit is considered negative if it is 1)

  2. If the number is negative, it will be converted to the complement code.

The sample code is as follows:

 <?php
function bindec_signed($bin) {
    $num = bindec($bin);
    $bits = strlen($bin);
    // Determine whether the highest bit is1(Negative number flag)
    if ($bin[0] === '1') {
        // Calculate the negative value of the complement code
        $num -= (1 << $bits);
    }
    return $num;
}

// test
echo bindec_signed("11111111");  // Output -1
echo "\n";
echo bindec_signed("10000000");  // Output -128
echo "\n";
echo bindec_signed("01111111");  // Output 127
?>

This function uses the mathematical principle of complement code to judge the highest bit of a binary number and make correct positive and negative conversions.


Summarize

  • The bindec() function uses binary strings as unsigned numbers by default, and the output result is always positive integers or zeros.

  • For two's complement that represents negative numbers, bindec() cannot directly give the correct negative values, and needs to be processed extra by itself.

  • The correct conversion of negative numbers can be achieved by judging the highest bit and combining the complement calculation.