【问题标题】:Multiply two integers without using any arithmetic operators不使用任何算术运算符将两个整数相乘
【发布时间】:2021-03-02 04:14:23
【问题描述】:

我已经设法在不使用任何算术运算符的情况下加减两个整数。我尝试过将两个整数相乘的乘法方法,但似乎没有任何进展。如何在不使用任何算术运算符的情况下将两个整数相乘?不能使用的算术运算符是 (+, ++, +=, -, --, -=, ∗, /, %)。同样从给出的指示是“取产品的最低 16 位应该是 存储在产品中,完整的产品应该作为 32 位值存储在 full_product 中。”方法中注释掉的行是为了告诉你什么不能做。谢谢!这是用 C 完成的代码

#include "alu.h"

/* Adds the two arguments and stores the sum in the return structure's result
 * field.  If the operation overflowed then the overflow flag is set. */
addition_subtraction_result add(uint16_t augend, uint16_t addend) {
    addition_subtraction_result addition;
    while (addend != 0){
      int carry = augend & addend;
      augend = augend ^ addend;
      addend = carry << 1;
    }
    addition.result = augend;
    //addition.overflow = false;
    return addition;
}

/* Subtracts the second argument from the first, stores the difference in the
 * return structure's result field.  If the operation overflowed then the
 * overflow flag is set. */
addition_subtraction_result subtract(uint16_t menuend, uint16_t subtrahend) {
    addition_subtraction_result subtraction;
    while (subtrahend != 0 ){
      int borrow = (~menuend) & subtrahend;
        menuend = menuend ^ subtrahend;
        subtrahend = borrow << 1;
    }
    subtraction.result = menuend;
    return subtraction;
}

/* Multiplies the two arguments.  The function stores lowest 16 bits of the
 * product in the return structure's product field and the full 32-bit product
 * in the full_product field.  If the product doesn't fit in the 16-bit
 * product field then the overflow flag is set. */
multiplication_result multiply(uint16_t multiplicand, uint16_t multiplier) {
    multiplication_result multiplication;
    //multiplication.product = multiplicand * multiplier;         // THIS IS DISALLOWED
    //multiplication.full_product = multiplicand * multiplier;    // THIS IS DISALLOWED
                          
    multiplication.product = multiplicand;
    multiplication.full_product = multiplicand;
    return multiplication;
}

【问题讨论】:

  • 提示:乘法是重复加法。
  • “我尝试了将两个整数相乘的乘法方法,但似乎没有任何进展” --> 如果您至少发布了其中一个,这将很有用。

标签: c unsigned-integer


【解决方案1】:

总体思路(类型故意不正确,您无法将其复制/粘贴回去):

uint16_t multiply(uint8_t multiplicand, uint8_t multiplier)
{
    uint16_t result = 0;
    for (int i = 0; i < CHAR_BIT * sizeof(multiplier); i++)
        if (multiplier & (uint8_t))
            result = add(result, (uint16_t)multiplicand << (uint16_t)i);
    return result;
}

但这还不适合你,因为你还没有长添加。我们需要像这样分解长添加:

uint16_t addlong(uint8_t addend1a, uint8_t addend1b, uint8_t addend2a, uint8_t addend2b)
{
    struct addend_result a = add(addend1a, addend2a);
    struct addend_result b = add(addend2a, addend2b);
    if (a.carry) b = add(b.result, 1);
    return a.result | ((uint16_t)b.result << 8);
}

所以这些是构建所需的部分。使它们适应您实际拥有的框架和您实际拥有的类型宽度。

由于愚蠢,您必须多次展开 for 循环。这意味着你得到 16 行,因为你的输入大小是 16 位。

【讨论】:

  • if (multiplier &amp; (uint8_t)) 中的错字(应为if (multiplier &amp; (uint8_t)1))。循环本身可能更像while(multiplier != 0) {multiplier &gt;&gt;= 1; multiplicand &lt;&lt;= 1;,以避免需要i 和(不允许的)i++
猜你喜欢
  • 2011-05-26
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 2016-02-22
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多