PHP uses bit operations to implement addition, subtraction, multiplication and division of integers and test them (code example)

不言
Release: 2023-04-03 18:24:02
Original
3173 people have browsed it

The content of this article is about PHP using bit operations to implement addition, subtraction, multiplication and division of integers and testing (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

<?php
/**
 * Created by PhpStorm.
 * User: Mch
 * Date: 8/10/18
 * Time: 23:51
 * 只用位运算不用算数运算实现整数的 + - * /
 */
class Arithmetic {
    const MAX_INTEGER = 2147483647;
    const MIN_INTEGER = -2147483648;
    /**
     * @param int $a
     * @param int $b
     * @return int  $a + $b;
     */
    public static function add(int $a, int $b) : int {
        $sum = $a;
        while ($b) {
            $sum = $a ^ $b;       // 不考虑进位
            $b = ($a & $b) << 1;  //  只考虑进位
            $a = $sum;
        }
        return $sum;
    }

    /**
     * 相反数 <= 二进制表达取反+1(补码)
     * @param int $n
     * @return int
     */
    private static function negateNumber(int $n) : int {
        return self::add(~$n, 1);
    }

    /**
     * a-b = a + (-b)
     * @param int $a
     * @param int $b
     * @return int
     */
    public static function minus(int $a, int $b) : int {
        return self::add($a, self::negateNumber($b));
    }

    /**
     * @param int $a
     * @param int $b
     * @return int  $a * $b
     */
    public static function multiple(int $a, int $b) : int {
        $res = 0;
        while ($b) {
            if (($b & 1)) {
                $res = self::add($res, $a);
            }
            $a <<= 1;
            $b >>= 1;
        }
        return $res;
    }

    private static function isNegative(int $n) : bool {
        return $n < 0;
    }

    /**
     * a/b  a = MIN_INTEGER, b!=MIN_INTEGER ?
     * @param int $a
     * @param int $b
     * @return int
     */
    private static function p(int $a, int $b) : int {
        $x = self::isNegative($a) ? self::negateNumber($a) : $a;
        $y = self::isNegative($b) ? self::negateNumber($b) : $b;
        $res = 0;
        for ($i = 31; $i >-1; $i = self::minus($i, 1)) {
            if (($x >> $i)>=$y) {
                $res |= (1 << $i);
                $x = self::minus($x, $y<<$i);
            }
        }
        return self::isNegative($a) ^ self::isNegative($b) ? self::negateNumber($res):$res;
    }

    /**
     * @param int $a
     * @param int $b
     * @return int $a / $b
     */
    public static function pide(int $a, int $b) : int {
        if ($b === 0) {
            throw new RuntimeException("pisor is 0");
        }
        if ($a === self::MIN_INTEGER && $b === self::MIN_INTEGER) {
            return 1;
        } else if ($b === self::MIN_INTEGER) {
            return 0;
        } else if ($a === self::MIN_INTEGER) {
            $res = self::p(self::add($a, 1), $b);
            return self::add($res, self::p(self::minus($a, self::multiple($res, $b)), $b));
        } else {
            return self::p($a, $b);
        }
    }
}
Copy after login

TEST:

echo Arithmetic::add(1, 2).PHP_EOL;  // 3
echo Arithmetic::minus(10, 3).PHP_EOL;  // 7
echo Arithmetic::multiple(5, 3).PHP_EOL;  // 15
echo Arithmetic::pide(-2147483648, 1).PHP_EOL;  // -2147483648
echo Arithmetic::pide(-15, 3).PHP_EOL;  // -5
Copy after login

Related recommendations:

Code for simple interaction between PHP and html forms

phpHow to generate classes for HTML files? How to generate html file class in php

The above is the detailed content of PHP uses bit operations to implement addition, subtraction, multiplication and division of integers and test them (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
bit
source:php.cn
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