Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.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 - Fatal编程技术网

C 编写一个程序,不使用算术运算符将两个数相乘

C 编写一个程序,不使用算术运算符将两个数相乘,c,C,可能重复: 是否可以编写一个程序(用C语言)将两个数字相乘,而不使用任何算术运算符(*、+、-、/、%)。如果您可以使用位运算符进行加法,下面的代码片段可能会有所帮助 int xor, and, temp; and = x & y; xor = x ^ y; while(and != 0 ) { and <<= 1; temp = xor ^ and; and &= xor; xor = temp; } int-xor,and,temp; 和=x

可能重复:


是否可以编写一个程序(用C语言)将两个数字相乘,而不使用任何算术运算符(*、+、-、/、%)。

如果您可以使用位运算符进行加法,下面的代码片段可能会有所帮助

int xor, and, temp;
and = x & y;
xor = x ^ y; 

while(and != 0 )
{
 and <<= 1; 
 temp = xor ^ and;
 and &= xor; 
 xor = temp; 
}
int-xor,and,temp;
和=x&y;
xor=x^y;
while(and!=0)
{

当然,如果图灵机器能做到,C也能做到(只要你有足够的内存)。但你可能不会看到我写它

如果你想自己做这件事,一种方法是模拟你如何在纸上把两个数字相乘。你可以象征性地处理这些数字。你只需要处理每个数字和一个将这些数字相乘的结果表

对于两个数字的相加,可以使用类似的“相加”表


无论您使用的数字是十进制数字还是二进制数字,原理都是一样的。

您可以使用图灵机将两个数字相乘,可以说图灵机不使用任何算术运算符,只需将磁带向左/向右移动,在磁带上添加/删除标记。因此,您可以编写一个模拟t的C程序使用乘数。

查看。

以下是如何使用Farmer算法进行操作。
add()
来自Neera在我面前给出的答案:

#include <stdio.h>

unsigned int add(unsigned int x, unsigned int y)
{
        unsigned int xor, and, temp;
        and = x & y;
        xor = x ^ y;
        while(and != 0 ) {
                and <<= 1;
                temp = xor ^ and;
                and &= xor;
                xor = temp;
        }
        return xor;
}

int main()
{
        unsigned int multiplicand = 41,
                     multiplier = 6,
                     res = 0;

        while(multiplier != 0) {
                if (multiplier & 1) {
                        res = add(res, multiplicand);
                }
                multiplier >>= 1;
                multiplicand <<= 1;
        }
        printf("6 times 41 equals %u\n", res);
        return 0;
}
#包括
无符号整数相加(无符号整数x,无符号整数y)
{
无符号整数异或,和,温度;
和=x&y;
xor=x^y;
while(and!=0){
and=1;

被乘数,结果在哪里?是在xor中吗?如果是这样的话,它看起来像2*1=3,实际上将它与我的解结合起来是可行的。你将其中一个数分解为2的幂,将第二个数乘以每一个数,然后将所有结果相加。重复:答案非常惊人though@SiegeX:如何将两个数相乘“如何添加两个数字”的副本。参考问题中给出的答案也回答了这个问题,但这并不意味着问题是重复的。
#include <stdio.h>

unsigned int add(unsigned int x, unsigned int y)
{
        unsigned int xor, and, temp;
        and = x & y;
        xor = x ^ y;
        while(and != 0 ) {
                and <<= 1;
                temp = xor ^ and;
                and &= xor;
                xor = temp;
        }
        return xor;
}

int main()
{
        unsigned int multiplicand = 41,
                     multiplier = 6,
                     res = 0;

        while(multiplier != 0) {
                if (multiplier & 1) {
                        res = add(res, multiplicand);
                }
                multiplier >>= 1;
                multiplicand <<= 1;
        }
        printf("6 times 41 equals %u\n", res);
        return 0;
}