Home > Backend Development > PHP Tutorial > PHP Program for Binary to Decimal Conversion

PHP Program for Binary to Decimal Conversion

Linda Hamilton
Release: 2025-02-07 11:21:09
Original
632 people have browsed it

PHP Program for Binary to Decimal Conversion

Binary to decimal conversion refers to the process of converting a binary number (i.e., a number represented by only two digits 0 and 1) to its equivalent decimal number (i.e., in the form of 10 as a base).

This article will explain how to convert the binary form of a number to a decimal form in PHP using different methods.

How to convert binary to decimal?

Binary numbers (Binary ) are composed only of 0 and 1 digits used by machines, and decimal numbers are decimal numbers used by humans. To convert a binary number to a decimal number, multiply each binary number by its position power (starting from 0 on the right), and add all the results.

Example 1

  • Input: Binary number = 101
  • Output: Decimal number = 5
Explanation:

Binary number 101 can be converted to decimal number, the method is as follows: (1 × 2

2) (0 × 21) (1 × 20) = 4 0 1 = 5

Example 2

  • Input: Binary number = 1111
  • Output: Decimal number = 15
Explanation:

Binary number 1111 can be calculated as follows: (1 × 2

3) (1 × 22) (1 × 21) (1 × 20) = 8 4 2 1 = 15

Example 3

  • Input: Binary number = 0
  • Output: Decimal number = 0
Explanation:

Binary number 0 corresponds to decimal number 0, because the contribution value of all numbers is 0.

The following are different ways to perform binary to decimal conversion in PHP:

Binary to decimal conversion using built-in functions

PHP has a built-in function

bindec(), which can be used directly to convert binary numbers to decimal numbers.

Implementation steps

    Define binary numbers as input.
  1. Use the
  2. bindec() function to convert binary numbers to decimal form.
  3. Output result.
Implementation code

<?php
$binary = "101";

// 使用 bindec() 将二进制转换为十进制
$decimal = bindec($binary);

echo "二进制数 '$binary' 的十进制等效值为:$decimal";
?>
Copy after login
Copy after login
Output

<code>二进制数 '101' 的十进制等效值为:5</code>
Copy after login
Copy after login
Copy after login

Time complexity: O(1) Space complexity: O(1)

Binary to decimal conversion using loops

In this method, we use a loop to traverse binary numbers from left to right. We calculate the decimal equivalent by adding the result of multiplying each binary number with its position value.

Implementation steps

  1. First, define the binary number as a string input.
  2. Now, initialize a variable to store the decimal result.
  3. Use a loop to traverse binary numbers from right to left.
  4. Multiple each binary number by the power of position by 2 and add it to the result.
  5. Output the final decimal result.

Implementation code

<?php
$binary = "101";

// 使用 bindec() 将二进制转换为十进制
$decimal = bindec($binary);

echo "二进制数 '$binary' 的十进制等效值为:$decimal";
?>
Copy after login
Copy after login

Output

<code>二进制数 '101' 的十进制等效值为:5</code>
Copy after login
Copy after login
Copy after login

Time complexity: O(n) Space complexity: O(1)

Use bit operator

In this method, we directly use the bit operators supported by PHP to operate the bits. In this approach, we use displacement to calculate the decimal equivalent.

Implementation steps

  1. First, we initialize the variable of the decimal result to 0.
  2. Now, loop through the binary string, starting with the least significant bit.
  3. For each bit, shift left result and add the value of the current bit.
  4. Return the final result after processing all bits.

Implementation code

<?php
$binary = "101";
$decimal = 0;
$length = strlen($binary);

// 循环遍历二进制数中的每个数字
for ($i = 0; $i < $length; $i++) {
    // 将二进制数字乘以 2^(从右起的位置)
    $decimal += $binary[$length - $i - 1] * pow(2, $i);
}

// 输出十进制等效值
echo "二进制数 '$binary' 的十进制等效值为:$decimal";
?>
Copy after login

Output

<code>二进制数 '101' 的十进制等效值为:5</code>
Copy after login
Copy after login
Copy after login

Time complexity: O(n) Space complexity: O(1)

Practical application of binary to decimal conversion

  • It is used to perform many programming tasks that take binary to decimal conversion as part of the encoding or decoding process.
  • IP address, subnet mask, and MAC address sometimes require binary and decimal conversion for configuration and analysis.

The above is the detailed content of PHP Program for Binary to Decimal Conversion. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template