12,759,139 members (33,830 online)
Rate this:
See more:
I wrote this code for simulating multiplication of 4 bit binary numbers stored in arrays BeforeDecimal1 & BeforeDecimal2. The result of multiplication is stored in arrays s[]. The problem is this code isn't providing correct answer. I tried to figure out the problem but worthless, so now I seek the advice of experts.Here is the code for manipulating Long Hand Multiplication:

I am trying to do this:

```1000

x

1110
-------------------

0000

1000

1000

1000
-------------
= 1110000```

```int i=3,x=0,carry=0;
while(true)
{
if(BeforeDecimal2[i]!=0)
{
for(int j=x;j>=0;j--)
{
if(s[j]==1 && BeforeDecimal1[j]==1 && carry==0)
{
carry=1;
s[j]=0;
}
else if(s[j]==1 && BeforeDecimal1[j]==0 && carry==1)
{
carry=1;
s[j]=0;
}
else if(s[j]==0 && BeforeDecimal1[j]==0 && carry==1)
{
carry=0;
s[j]=1;
}
else if(s[j]==0 && BeforeDecimal1[j]==0 && carry==0)
{
carry=0;
s[j]=0;
}

x++;
}
}
else
{
for(int h=7;h>=0;h--)
{
if(h==0)
{
BeforeDecimal1[0]=0; // that is inserting zeros from the right
}
else
{
BeforeDecimal1[h]=BeforeDecimal1[h-1];
BeforeDecimal1[h-1]=0;
}
}
}
if(i==0)
break;

i--;
}```

Regards

Indexation and else if in the same line
Posted 1-Dec-12 22:36pm
Updated 2-Dec-12 9:04am
Nelek115.7K
v3
Philippe Mori 2-Dec-12 8:36am

First of all, it look like you are running your loop in the wrong direction. You should start your multiplication with least significant bits.

Then your if does handles all cases. With 3 bits, you have 8 cases to handle. And it whould be much easier if you would check if you have 2 or more 1 bit for the carry and do an XOR for the other bit.
Nelek 2-Dec-12 13:57pm

When posting code that goes so deep in the indexation. You should "search and replace" tabulators by two or three "empty spaces". It will increase the readability a lot, making it easier for other users to solve your problem/question.
Another point, get used to always use the brackets, specially if you want to make such nested constructions. You can mess things up very easy if you don't, because you loose the scope of what is being executed and what not.
else
if

and use the correct
else if

I have edited the code. Please have a look now, to see the points I mean.

Rate this:

Solution 1

I'm sorry to say this, but...that is pretty much unreadable.

I'm not sure what algorithm you are using, but I doubt if it included quite so many nested if's.
I think the first thing you need to do is look at revising the indentation of that lot, because it is seriously confusing at the moment.
Try starting by changing:
```if(reverse==4 && carry==1 && temp==0)
{
s[reverse]=1;
}
else
if(reverse==4 && carry==0 && temp==1)
{
s[reverse]=1;
}
else
if(reverse==4 && carry==0 && temp==0)```
To
```if(reverse==4 && carry==1 && temp==0)
{
s[reverse]=1;
}
else if(reverse==4 && carry==0 && temp==1)
{
s[reverse]=1;
}
else if(reverse==4 && carry==0 && temp==0)```
So that it is clearer without running off the right hand side of the page.
Then look at commenting - I have absolutely no idea why you are doing the tests you are, and without any idea of the algorithm you are trying to implement I can't work it out from that code.

Sorry, but this need to be made readable, and explained a bit better before I can help!
Rate this:

Solution 2

Bit long, but should work:
```#include <stdio.h>

void mul(int bd1[4], int bd2[4], int s[8])
{
int i,j;

for (i=0;i<8;i++)
s[i] = 0;

for (i=0; i<4; i++)
{
int c = 0;
if ( bd2[3-i] )
{
for (j=0; j<4; j++)
{
if ( s[i+3-j] == 1)
{
if (bd1[3-j] == 1)
{
if (c==1)
{
s[i+3-j] = 1;
c = 1;
}
else
{ //c == 0
s[i+3-j] = 0;
c = 1;
}
}
else
{ // bd1[3-j] == 0
if (c==1)
{
s[i+3-j] = 0;
c = 1;
}
else
{ //c == 0
s[i+3-j] = 1;
c = 0;
}
}
}
else
{// s[i+3-j] == 0
if (bd1[3-j] == 1)
{
if (c==1)
{
s[i+3-j] = 0;
c = 1;
}
else
{ //c == 0
s[i+3-j] = 1;
c = 0;
}
}
else
{ // bd1[3-j] == 0
if (c==1)
{
s[i+3-j] = 1;
c = 0;
}
else
{ //c == 0
s[i+3-j] = 0;
c = 0;
}
}
}
}
}
}
}

int main()
{
int i;
int bd1[]={1,0,0,0};
int bd2[]={1,1,1,0};
int s[8];
mul(bd1,bd2,s);

for(i=0;i<8;i++)
printf("%d", s[i]);
printf("\n");
return 0;
}```
v3

Top Experts
Last 24hrsThis month
 OriginalGriff 200 Bryian Tan 185 ppolymorphe 159 Graeme_Grant 158 Richard Deeming 150
 OriginalGriff 4,327 Peter Leow 3,279 ppolymorphe 2,683 Karthik Bangalore 2,594 Graeme_Grant 2,387