gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:11:26 25 4
gpt4 key购买 nike

确实需要帮助,因为该错误很小,而且几乎没有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com