How to implement addition in PHP without using addition, subtraction, multiplication and division symbols

醉折花枝作酒筹
Release: 2023-03-11 11:44:02
forward
2029 people have browsed it

We know that to implement addition we need to use arithmetic symbols, so how to implement addition without using arithmetic symbols? Today, the editor will introduce how to implement addition without using addition, subtraction, multiplication and division operators. You can refer to it if you need it.

How to implement addition in PHP without using addition, subtraction, multiplication and division symbols

Write a function to find the sum of two integers. It is required that the four arithmetic symbols " ", "-", "*" and "/" must not be used in the function body.

Example:

输入: a = 1, b = 1
输出: 2
Copy after login

Tips:

a, b may be negative or 0, the result will not overflow the 32-bit integer

Solve the problem Idea 1

array_sum function

Code

class Solution {
    /** 
    * 递归法 
    * @param Integer $a 
    * @param Integer $b 
    * @return Integer 
    */
    function add($a, $b) {
        // return bcadd($a, $b); // bc 系列函数
        return array_sum([$a, $b]);
    }}
Copy after login

Solution idea 2 - With the help of bit operations

this question bit It's better to memorize the operations. After all, the analog addition usage of bit operations is basically just this question, and it can be easily forgotten. . . . .

^ Or - equivalent to summation without carry, imagine the simulation situation in base 10: (for example: 19 1 = 20; sum without carry is 10, not 20; because it does not care Carry situation)

& and - are equivalent to finding the number of carries for each bit. First look at the definition: 1 & 1 = 1; 1 & 0 = 0; 0 & 0 = 0; that is, when both are 1 is only 1, which can just simulate the situation of the carry number, or imagine the simulation situation in base 10: (9 1 = 10, if it is processed with the idea of ​​​​&, then the carry number obtained by 9 1 is 1, not 10, So use <<1 to move one position to the left, so it becomes 10);

The formula is: (a^b) ^ ((a&b)<<1) That is : Find the number of carries each time without carry - We need to repeat this process until the number of carries is 0;

Code

class Solution {
    /** 
    * 递归法 
    * @param Integer $a 
    * @param Integer $b 
    * @return Integer 
    */
    function add($a, $b) {
        if ($b == 0) {
            return $a;
        }
        return $this->add($a ^ $b, ($a & $b) << 1);
    }
    // 迭代法
    // function add($a, $b) {
    // while ($b != 0) {
    // $temp = $a ^ $b;
    // $b = ($a & $b) << 1;
    // $a = $temp;
    // }
    // return $a;
    // }}
Copy after login

Problem-solving ideas 3 - Bit by bit calculation

The principle is equivalent to bit operation, but the implementation of manual bit operation

Code

class Solution {
    /** 
    * 此方法暂时只支持两个正数相加 
    * @param Integer $a 
    * @param Integer $b 
    * @return Integer 
    */
    function add($a, $b) {
        $a = "$a";
        $b = "$b";
        $lenA = strlen($a);
        $lenB = strlen($b);
        $res = &#39;&#39;;
        $flag = 0;   // 进位标志
        for ($i = $lenA - 1, $j = $lenB - 1; $i >= 0 || $j >= 0; $i--, $j--) {
            // 超出的位数用 0 表示
            $itemA = ($i >= 0) ? $a[$i] : 0;
            $itemB = ($j >= 0) ? $b[$j] : 0;
            // 求位数和
            $sum = (int)$itemA + (int)$itemB + $flag;
            // 是否进位
            if ($sum >= 10) {
                $flag = 1;  // 只可能为1
                $sum = $sum - 10;
            } else {
                $flag = 0;
            }
            
            $res = $sum . $res;
        }
        // 处理最高位进位
        return $flag > 0 ? $flag . $res : $res;
    }}
Copy after login

Recommended learning: php video tutorial

The above is the detailed content of How to implement addition in PHP without using addition, subtraction, multiplication and division symbols. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:hxd.life
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template