c - C 代码中的一个小错误,用于在不使用 * 运算符的情况下乘以 2 位二进制数

标签 c multiplication short

确实需要帮助,因为该错误很小,而且几乎没有 6-7 行代码。我已附上 .cpp 文件链接,该链接已被很好地注释以解释代码中的错误发生 1 [点击她获取代码]..请帮忙,因为我在这个问题上停留了大约一个小时或更长时间。.明天需要在N大学提交此信息!

/*Multiplication Of 2-Bit Binary Numbers Without Using Multiplication Operator*/

/*
    1   1
    1   1
    -----
    1   1
1   1   0
---------
(By Full Adder)
1   0   0   1

*/

#include<stdio.h>
#include<conio.h>

void main()
{

    int k=0,a[4],b[4],i,x,y,temp=0,temp1=0,temp2=0,ans[2],j=1,c=0,sum=0;

    clrscr();

    printf("Enter The 2-Bit Binary Number(1):\t");
    scanf("%d",&x);

    printf("Enter The 2-Bit Binary Number(2):\t");
    scanf("%d",&y);


    for(i=10;i<=100;i=i*10)
    {
     temp=y%i;
     temp=temp/(i/10);

     if(temp==1)
        {
         ans[j]=x;
        }

     else
        {
        ans[j]=0;
        }

     j++;

    }


    ans[2]=ans[2]*10;


    //Implementing Full Adder

    for(i=10;i<=10000;i=i*10)

        {

         //if anyone in comment, correct output

         /* considering second commented and input for x and y as 11
            the output for //1 would be
            0
            1
            1
            0
            0
         */  


        //if both //1 //2 uncommented, wrong output
        /*  0
            1
            0
            1
            0
            0
            0
            0
            0
            0
        */ 


         // 1
         temp2=ans[2]%i;
         temp2=temp2/(i/10);
         printf("%d\n",temp2);

         // 2
         temp1=ans[1]%i;
         temp1=temp1/(i/10);
         printf("%d\n",temp1);


         sum=(temp1^temp2)^c;
         c=(temp1&temp2)|((temp1^temp2)&c);

         b[k]=sum;
         k++;
        }


    for(i=0;i<4;i++)
    {
     printf("%d",b[i]);
    }


    getch();

}

最佳答案

void main()

应该是

int main()

main的返回类型为int。

编辑:

分析代码后,似乎您应该在最后一个循环中以自下而上的顺序进行异或。基本上,您正在反向打印答案。

所以:

for(i=0;i<4;i++)

应该是:

for(i=3;i>=0;i--)

关于c - C 代码中的一个小错误,用于在不使用 * 运算符的情况下乘以 2 位二进制数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22488236/

相关文章:

algorithm - 乘法算法的渐近复杂度是否仅依赖于两个操作数中较大的一个?

java - Largest Java Short (32767) 加 1 不转负?

c# - 将 byte[] 数组转换为长度减半的 short[] 数组

Java:在 Set 中添加/删除短整型元素时输出不同

c - 在 ubuntu 11.04 上使用 gcc 4.5.2 声明函数指针时出错

c - 如何在C中使用sscanf解析json?

c - 使用一个结构到另一个结构 c

在 C 中具有不断增长的堆栈的协程

Python、numpy、einsum 将一叠矩阵相乘

c++ - 使用 32 位无符号整数乘以 64 位数字的算法