Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 不使用任何算术运算符将两个整数相乘_C_Unsigned Integer - Fatal编程技术网

C 不使用任何算术运算符将两个整数相乘

C 不使用任何算术运算符将两个整数相乘,c,unsigned-integer,C,Unsigned Integer,我已经设法在不使用任何算术运算符的情况下加减两个整数。我试过用乘法的方法将两个整数相乘,但似乎没有任何进展。不使用任何算术运算符,如何将两个整数相乘?不能使用的算术运算符是(+、++、+=、-、-、-、-、-=,∗, /, %). 另外,从给出的指示来看,是“获取产品的最低16位应该是 存储在product中,完整的乘积应该作为32位值存储在full_product中。”方法中注释掉的行显示了不应该执行的操作。谢谢!下面是用C完成的代码 #include "alu.h" /

我已经设法在不使用任何算术运算符的情况下加减两个整数。我试过用乘法的方法将两个整数相乘,但似乎没有任何进展。不使用任何算术运算符,如何将两个整数相乘?不能使用的算术运算符是(+、++、+=、-、-、-、-、-=,∗, /, %). 另外,从给出的指示来看,是“获取产品的最低16位应该是 存储在product中,完整的乘积应该作为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;
}
#包括“alu.h”
/*添加两个参数并将总和存储在返回结构的结果中
*字段。如果操作溢出,则设置溢出标志*/
加减结果加(uint16加数,uint16加数){
加法\减法\结果加法;
while(加数!=0){
整数进位=加数和加数;
augend=augend^加数;
加数=进位总体思路(类型故意不正确,无法复制/粘贴):

uint16\u t乘法(uint8\u t乘法器,uint8\u t乘法器)
{
uint16_t结果=0;
对于(int i=0;iresult=add(result,(uint16_t)multiplier and提示:乘法是重复加法。“我尝试过用乘法方法将两个整数相乘,但似乎没有任何进展”-->如果您至少发布了其中一个,这是很有用的。键入
if(multiplier&(uint8_t))
(应该是
if(multiplier&(uint8_t)1)
)循环本身可以更像
而(乘数!=0){
乘数>>=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);
}