php中加减乘除的代码,php使用位运算实现整数的加减乘除并测试(代码示例)

本篇文章给大家带来的内容是关于php使用位运算实现整数的加减乘除并测试(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。<?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<

}

}

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);

}

}

}

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

相关推荐:

PHP和html表单之间实现简单交互的代码

php如何生成HTML文件的类?php生成html文件类的方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是大整数乘除运算的C语言手动实现完整代码: ``` #include <stdio.h> #include <string.h> #define MAX_LEN 1000 void reverse(char* str) { int len = strlen(str); for (int i = 0; i < len / 2; i++) { char temp = str[i]; str[i] = str[len - i - 1]; str[len - i - 1] = temp; } } void add(char* a, char* b, char* result) { int carry = 0; int i = 0, j = 0, k = 0; int len_a = strlen(a), len_b = strlen(b); while (i < len_a || j < len_b) { int x = (i < len_a) ? (a[i] - '0') : 0; int y = (j < len_b) ? (b[j] - '0') : 0; int sum = x + y + carry; result[k++] = (sum % 10) + '0'; carry = sum / 10; i++; j++; } if (carry) { result[k++] = carry + '0'; } result[k] = '\0'; reverse(result); } void subtract(char* a, char* b, char* result) { int borrow = 0; int i = 0, j = 0, k = 0; int len_a = strlen(a), len_b = strlen(b); while (i < len_a || j < len_b) { int x = (i < len_a) ? (a[i] - '0') : 0; int y = (j < len_b) ? (b[j] - '0') : 0; int diff = x - y - borrow; if (diff < 0) { diff += 10; borrow = 1; } else { borrow = 0; } result[k++] = diff + '0'; i++; j++; } while (k > 1 && result[k - 1] == '0') { k--; } result[k] = '\0'; reverse(result); } void multiply(char* a, char* b, char* result) { int len_a = strlen(a), len_b = strlen(b); int* temp = new int[len_a + len_b](); for (int i = len_a - 1; i >= 0; i--) { for (int j = len_b - 1; j >= 0; j--) { int x = a[i] - '0'; int y = b[j] - '0'; temp[i + j + 1] += x * y; } } for (int i = len_a + len_b - 1; i > 0; i--) { temp[i - 1] += temp[i] / 10; temp[i] %= 10; } int k = 0; while (k < len_a + len_b && temp[k] == 0) { k++; } for (int i = k; i < len_a + len_b; i++) { result[i - k] = temp[i] + '0'; } result[len_a + len_b - k] = '\0'; if (strlen(result) == 0) { strcpy(result, "0"); } delete[] temp; } void divide(char* a, char* b, char* result) { int len_a = strlen(a), len_b = strlen(b); if (len_a < len_b || (len_a == len_b && strcmp(a, b) < 0)) { strcpy(result, "0"); return; } char* temp = new char[len_a + 1]; char* quotient = new char[len_a + 1]; strcpy(temp, a); for (int i = 0; i < len_a - len_b + 1; i++) { strcpy(quotient, "0"); while (strcmp(temp, b) >= 0) { subtract(temp, b, temp); add(quotient, "1", quotient); } reverse(quotient); result[i] = quotient[0]; memmove(temp + 1, temp, strlen(temp) + 1); temp[0] = '0'; } result[len_a - len_b + 1] = '\0'; while (strlen(result) > 1 && result[0] == '0') { memmove(result, result + 1, strlen(result)); } delete[] temp; delete[] quotient; } int main() { char a[MAX_LEN], b[MAX_LEN], result[MAX_LEN]; printf("Enter the first number: "); scanf("%s", a); printf("Enter the second number: "); scanf("%s", b); add(a, b, result); printf("Sum = %s\n", result); subtract(a, b, result); printf("Difference = %s\n", result); multiply(a, b, result); printf("Product = %s\n", result); divide(a, b, result); printf("Quotient = %s\n", result); return 0; } ``` 希望这个代码能够帮助你实现整数乘除运算

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值