1,105,416 Community Members

multiplication of 4-bit no. by shift and add method

Member Avatar
gunjan.vora.9
Newbie Poster
1 post since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm writing a program to multiply two 4-bit number using add and shift method in C language. The code is as follows:

#include <stdio.h>
#include <math.h>

int main()
{
    int d,p=0, y=1010, x=1101, c, i;
    for(i=0;i<=3;i++)
    {
        c = x % 10;
        y = y << i;
        d = c*y;
        p = p ^ d;
        x = x / 10;
    }
    printf("%d",p);
    return 0;
}

I'm getting answer as 57570 which is wrong. Where am I going wrong?

Member Avatar
faysal.ishtiaq
Newbie Poster
5 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

shifting with 1 means multiplying by 2 in case of integer. shifting adds 0 left / right (depends on you) when the number is in binary. So, you can convert your integer into binary keeping the figure same. not the value. I mean integer 1010 will remain 1010 in binary. And then you can multiply using shifting operator. And it depends on you whwther you will print your result in binary or integer form.

Member Avatar
Tumlee
Posting Whiz in Training
203 posts since Oct 2011
Reputation Points: 42 [?]
Q&As Helped to Solve: 41 [?]
Skill Endorsements: 4 [?]
 
0
 

Where am I going wrong?

If you had given your variables descriptive names so you could easily read through what your code is doing, or at least provided comments illuding to your train of thought when writing this program, you likely wouldn't have to ask us this question.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article