4 bit Binary Array Multiplication - in C programming language

How we can Multiply 4 bit binary Numers??Plz Any One Write The Source code for it...plz guys help me....

Replies

  • Kaustubh Katdare
    Kaustubh Katdare
    SDKING
    How we can Multiply 4 bit binary Numers??Plz Any One Write The Source code for it...plz guys help me....
    You're trying to do this, right? -

          1011  (this is 11 in decimal)
        x 1110  (this is 14 in decimal)
        ======
          0000  (this is 1011 x 0)
          1011    (this is 1011 x 1, shifted one position to the left)
        1011    (this is 1011 x 1, shifted two positions to the left)
      + 1011      (this is 1011 x 1, shifted three positions to the left)
      =========
      10011010  (this is 154 in decimal)
    Via: Wikipedia.

    For the procedure, look here - #-Link-Snipped-#

    How about attempting to write code on your own and then ask people for help where you get stuck? What do you say?
  • SDKING
    SDKING
    yes right...but i also know that method ...but to create their source code the array is using to store that ans and that ans becomes wrong my array shifting method is not work properly....
  • simplycoder
    simplycoder
    SDKING
    yes right...but i also know that method ...but to create their source code the array is using to store that ans and that ans becomes wrong my array shifting method is not work properly....
    did you try using debugger or print statements, step in through code step by step.
    Try it with decimal numbers and as you say, check for the shifting method.

    Once that is correct, you can add in the binary rules.(Which are not that different.)

    If you still dont get it, show us your source code.
  • SDKING
    SDKING
    ok....
  • SDKING
    SDKING
    hi...
    @simplycoder-
    this my code in Turbo c++ compiler...
    /*code*/
    #include
    #include
    void main()
    {
    int bin1[4]={1,0,0,1},bin2[4]={0,1,1,1};//binary numbers
    int m1[8],ans[8];
    int c=0;
    clrscr();
    int i,j;
    cout<<"Binary Num1:-";
    for(i=0;i<4;i++)
    cout<=0;k--)
    {
    if(bin2[k]==1)
    {
    for(j=3;j>=0;j--)
    m1[k+j+1]=bin1[j];
     
    }
    else if(bin2[k]==0)
    {
    for(j=3;j>=0;j--)
    m1[k+j+1]=0;
    }
     
     
     
    for(int i=0;i<8;i++)
    {
     
    *m1<
                                        
  • simplycoder
    simplycoder
    This code is unreadable. I note that you have mentioned comments and used prints to output where you want to debug.

    However the code is full of magic numbers and has no functions involved.
    In my opinion, you should write functions.
    1)For Initialization.
    2)For Multiplication.
    3)For Display.

    Also note that using magic numbers is bad practice. Initialize a constant first and then use that constant only.

    I think you should do this first then we can debug around.

    PS: If possible avoid using Turbo C++, use codeblocks or devc++ instead.
  • SDKING
    SDKING
    How we can Multiply 4 bit binary Numers??Plz Any One Write The Source code for it...plz guys help me....
  • SDKING
    SDKING
    /*this is my code and i want to multiplication of 2 4 bit binary numbers*/

    /*code*/
    #include
    #include
    void main()
    {
    int bin1[4]={1,0,0,1},bin2[4]={0,1,1,1};//binary numbers
    int m1[8],ans[8];
    int c=0;
    clrscr();
    int i,j;
    cout<<"Binary Num1:-";
    for(i=0;i<4;i++)
    cout< cout<
    cout<<"Binary Num1:-";
    for(i=0;i<4;i++)
    cout< cout<
    for(i=0;i<8;i++)
    {
    m1=ans=0;//initialize partial product,result array to the zero
    }
    for(int k=3;k>=0;k--)
    {
    if(bin2[k]==1)
    {
    for(j=3;j>=0;j--)
    m1[k+j+1]=bin1[j];

    }
    else if(bin2[k]==0)
    {
    for(j=3;j>=0;j--)
    m1[k+j+1]=0;
    }

    for(int i=0;i<8;i++)
    {

    *m1< /*array addition */
    if(m1==0&&ans==0&&c==0)
    {
    ans=0;
    c=0;
    }
    else if(m1==0&&ans==1&&c==0)
    {
    ans=1;
    c=0;
    }
    else if(m1==1&&ans==0&&c==0)
    {
    ans=1;
    c=0;
    }
    else if(m1==1&&ans==1&&c==0)
    {
    ans=0;
    c=1;
    }
    else if(m1==0&&ans==0&&c==1)
    {
    ans=1;
    c=0;
    }
    else if(m1==0&&ans==1&&c==1)
    {
    ans=0;
    c=1;
    }
    else if(m1==1&&ans==0&&c==1)
    {
    ans=0;
    c=1;
    }
    else if(m1==1&&ans==1&&c==1)
    {
    ans=1;
    c=1;
    }
    }
    }
    cout<<"\nMultiplication:-";
    for(i=0;i<8;i++)
    {
    cout<<""< }
    getch();
    }
  • Sanyam Khurana
    Sanyam Khurana
    wait buddy, let me understand your code,

    BDW, you have posted in wrong section..!! 😛
  • SDKING
    SDKING
    Sorry Buddy...my mistake...
  • Sanyam Khurana
    Sanyam Khurana
    Why have you made it so tedious, using array, and instead of simple switch, so many if else, it would just lead to more time in compiling your code.

    You could do it without use of array.
  • SDKING
    SDKING
    realy...how???....can you explain plz??
  • Sanyam Khurana
    Sanyam Khurana
    Check PM, and we could just PM everything😉

    Dual posting is being done!!! LOL
  • Kaustubh Katdare
    Kaustubh Katdare
    Posts moved to the most appropriate thread.
  • Sanyam Khurana
    Sanyam Khurana
    haha, thank you Sir😀

    #-Link-Snipped-# I hope now I have given you the right solution..👍

    All the best..!!
  • SDKING
    SDKING
    Yes sir....Thnx for suggestions....
    But Problem Is..
    @Sanyam- The Multiplication Must be using array bcoz the Algo which is giving me to implement that is in computer Orgnization by Refferance book Hassy is giving in Array format so i try with Array...So plz logic and code will be using array in c++ language....Meet you PM buddy...i hope you shall try with array also.... plzzz..
  • Sanyam Khurana
    Sanyam Khurana
    If you have the algo, then what is the problem you are facing #-Link-Snipped-# ?
  • SDKING
    SDKING
    My problem is about array shifting....the array is not shifting properly...
    when i run the above module the even number multiplication is done but the odd number multiplication is not work...
  • SDKING
    SDKING
    #-Link-Snipped-#- check the algo which is given above by kaustubh katdare sir...
    my algo is also same i am trying to implement that algo....with c++...👎
  • rahul69
    rahul69
    SDKING
    My problem is about array shifting....the array is not shifting properly...
    when i run the above module the even number multiplication is done but the odd number multiplication is not work...
    Seeing your code I will have to say what #-Link-Snipped-# had already said. Also one more thing, you must learn to indent your code.
    However, I am going to make some modifications in your code to make it work and will paste it here, but note that you should follow the best practices and avoid messy coding.😀
  • rahul69
    rahul69
    #include
    #include
     
    void main()
    {
    int bin1[4]={0,0,1,1},bin2[4]={1,1,0,1};//binary numbers
    int m1[8],ans[8];
    int c=0;
    clrscr();
    int i,j,k;
    cout<<"Binary Num1:-";
    for(i=0;i<4;i++)
      cout<=0;i--)
        {
        for(j=0;j<8;j++)
            {
            m1[j]=0;
            }
        if(bin1[i]==1)
          {  k=7;
     
              for(j=3;j>=0;j--)
            {
            m1[k]=bin2[j]*bin1[i];
     
            k--;
            }
              for(j=k;j<8;j++)
              {
              m1[j-(3-i)]=m1[j];
              }
              for(k=j-(3-i);k<8;k++)
              m1[k]=0;
              for(j=0;j<8;j++)
              cout<=0;j--)
          {
            if((ans[j]==1)&&(m1[j]==1)&&(c==0))
            {
            ans[j]=0;c=1;
            }
            else if((ans[j]==1)&&(m1[j]==1)&&(c==1))
            ans[j]=1;
            else if(((ans[j]==1)&&(m1[j]==0)||((ans[j]==0)&&(m1[j]==1)))&&c==1)
            ans[j]=0;
            else if ((ans[j]==0)&&(m1[j]==0)&&c==1)
            {ans[j]=1;c=0;}
            else
            {
            c=0;
            ans[j]+=m1[j];
            }
     
     
     
          }
     
        }
     
    cout<<"\nMultiplication:-";
    for(i=0;i<8;i++)
    {
    cout<<""<#-Link-Snipped-# Do keep the above points in mind 
    and,.. Happy Coding👍👍
  • SDKING
    SDKING
    thanx....guys...
  • Sanyam Khurana
    Sanyam Khurana
    Agree with #-Link-Snipped-#
  • simplycoder
    simplycoder
    /**********************************************************************************************/
    /***********************Multiplication of 4 bit binary Numbers.********************************/
    /**********************************************************************************************/
    /**********************************************************************************************/
    // Author    :  simplycoder
    // Date      :  29/03/2013
    // IDE      :  Code::Blocks 12.11
    /**********************************************************************************************/
     
    ///Region of macros - start.
    #include
    ///Region of macros - end.
    using namespace std;
    ///Region of Global variables - start
    const int numberOfBitsInANumber=4;
    const 
    int numberOfBitsInAnswer=2*numberOfBitsInANumber;
    int numberOneBinary[numberOfBitsInANumber];
    int numberTwoBinary[numberOfBitsInANumber];
    int answer[numberOfBitsInAnswer];
    int base=2;
    int highestDigit=0;
    ///Region of Global variables - end
     
    ///Region of function declaration - start.
    void Initialize();
    void TakeInputFromUser();
    void MultiplicationLogic();
    void PrintAnswer();
    ///Region of function declaration - end.
     
    ///Main entry of the program.
    int main(int argc,char**argv)
    {
        
    Initialize();
        
    TakeInputFromUser();
        
    MultiplicationLogic();
        
    PrintAnswer();
     
        return 
    0;
    }
     
    ///Initializes the input and the output arrays.
    void Initialize()
    {
        
    int index;
        for(
    index=0index<numberOfBitsInANumberindex++)
        {
            
    numberOneBinary[numberOfBitsInANumber]=0;
            
    numberTwoBinary[numberOfBitsInANumber]=0;
            
    answer[numberOfBitsInAnswer]=0;
        }
        
    highestDigit=base-1;
    }
     
    /// Takes the input number from the screen.
    void TakeInputFromUser()
    {
        
    int index;
        
    int numberOne=0;
        
    int numberTwo=0;
     
        
    cout<<"Please Enter the Numbers In 4-bit binary form: "<<endl;
     
        
    cin>>numberOne;
        
    cin>>numberTwo;
     
        for(
    index=0index<numberOfBitsInANumberindex++)
        {
            
    numberOneBinary[index]=numberOne%10;
            
    numberOne=numberOne/10;
     
            
    numberTwoBinary[index]=numberTwo%10;
            
    numberTwo=numberTwo/10;
        }
     
     
    }
     
    ///Logic for multiplication of numbers.
     
    void MultiplicationLogic()
    {
        
    int indexOne// Index for numberOneBinary.
        
    int indexTwo// Index for numberTwoBinary.
        
    int answerIndex=0//Index for answer.
        
    int isCarry=0// Logic high if carry else logic low.
        
    int incr=1;  // used  to adjust carry.
     
        
    for(indexOne=0indexOne<numberOfBitsInANumberindexOne++)
        {
            
    answerIndex=indexOne;
            for(
    indexTwo=0indexTwo<numberOfBitsInANumberindexTwo++)
            {
                
    answer[answerIndex+indexTwo]=answer[answerIndex+indexTwo]+(numberOneBinary[indexOne]*numberTwoBinary[indexTwo]);
                if(
    answer[answerIndex+indexTwo]>highestDigit)
                {
                    
    isCarry=1;
                    
    incr=indexTwo;
                    while(
    isCarry)
                    {
                        
    answer[answerIndex+incr+1]+=(answer[answerIndex+incr]/base);
                        
    answer[answerIndex+incr]%=base;
                        if(
    answer[answerIndex+incr]>highestDigit)
                        {
                            
    isCarry=1;
                            
    incr++;
                        }
                        else
                            
    isCarry=0;
                    }
                }
            }
        }
     
     
    }
     
    /// Logic to print the answer.
    void PrintAnswer()
    {
        
    int index=0;
        
    // Printing the MSB first.
        
    for(index=numberOfBitsInAnswer-1index>=0index--)
        {
            
    cout<<answer[index];
        }
        
    cout<<endl;
    }
    This is what I could come up with in 20 mins.
    There are few things which can be optimized, but due to constraints of time and readability, I haven't really optimized the code.
  • SDKING
    SDKING
    Right guys.....thnx....
  • SDKING
    SDKING
    Hey guy's my project is complete and working properly....
    thanx to all of you for helping me....

You are reading an archived discussion.

Related Posts

It all began with the reports of Apple developing a smart, connected wrist-watch that would be a handy companion to your smartphone or tablet. The iWatch was taken up by...
in whay way digital image processing is related to ece.
Hello Friends I am in search of a individual with great knowledge of various mobiles, gadgets etc and always remain in sync with upcoming technologies If interested then let me...
Today our CEan Praveen-Kumar is celebrating his birthday. Let's wish him a rocking year ahead. Many Many Happy Returns Of The Day PraveenKumar 🎉
Info About Robotics I have No Idea About Robot..... I want to devlop a robot which is using in robo war's...