CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@SDKING • 21 Mar, 2013
How we can Multiply 4 bit binary Numers??Plz Any One Write The Source code for it...plz guys help me....
@Kaustubh Katdare • 21 Mar, 2013
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 - https://www.swarthmore.edu/NatSci/echeeve1/Ref/BinaryMath/BinaryMath.html

How about attempting to write code on your own and then ask people for help where you get stuck? What do you say?
@SDKING • 22 Mar, 2013 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 • 25 Mar, 2013
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 • 26 Mar, 2013 ok....
@SDKING • 26 Mar, 2013 hi...
@simplycoder-
this my code in Turbo c++ compiler...
```/*code*/
#include<iostream.h>
#include<conio.h>
void main()
{
int bin1={1,0,0,1},bin2={0,1,1,1};//binary numbers
int m1,ans;
int c=0;
clrscr();
int i,j;
cout<<"Binary Num1:-";
for(i=0;i<4;i++)
cout<<bin1;
cout<<endl;

cout<<"Binary Num1:-";
for(i=0;i<4;i++)
cout<<bin2;
cout<<endl;

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<<i;//array shifting by i;
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<<""<<ans;
}
getch();
} ```
@simplycoder • 27 Mar, 2013 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 • 28 Mar, 2013 How we can Multiply 4 bit binary Numers??Plz Any One Write The Source code for it...plz guys help me....
@SDKING • 28 Mar, 2013 /*this is my code and i want to multiplication of 2 4 bit binary numbers*/

/*code*/
#include<iostream.h>
#include<conio.h>
void main()
{
int bin1={1,0,0,1},bin2={0,1,1,1};//binary numbers
int m1,ans;
int c=0;
clrscr();
int i,j;
cout<<"Binary Num1:-";
for(i=0;i<4;i++)
cout<<bin1;
cout<<endl;

cout<<"Binary Num1:-";
for(i=0;i<4;i++)
cout<<bin2;
cout<<endl;

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<<i;//array shifting by i;
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<<""<<ans;
}
getch();
}
@Sanyam Khurana • 28 Mar, 2013 wait buddy, let me understand your code,

BDW, you have posted in wrong section..!! 😛
@SDKING • 28 Mar, 2013 Sorry Buddy...my mistake...
@Sanyam Khurana • 28 Mar, 2013 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 • 28 Mar, 2013 realy...how???....can you explain plz??
@Sanyam Khurana • 28 Mar, 2013 Check PM, and we could just PM everything😉

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

SDKING I hope now I have given you the right solution..👍

All the best..!!
@SDKING • 28 Mar, 2013 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 • 28 Mar, 2013 If you have the algo, then what is the problem you are facing SDKING ?
@SDKING • 28 Mar, 2013 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 • 28 Mar, 2013 Sanyam Khurana- 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 • 29 Mar, 2013 • 1 like
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 simplycoder 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 • 29 Mar, 2013
```#include<iostream.h>
#include<conio.h>

void main()
{
int bin1={0,0,1,1},bin2={1,1,0,1};//binary numbers
int m1,ans;
int c=0;
clrscr();
int i,j,k;
cout<<"Binary Num1:-";
for(i=0;i<4;i++)
cout<<bin1[i];
cout<<endl;

cout<<"Binary Num1:-";
for(i=0;i<4;i++)
cout<<bin2[i];
cout<<endl;

/* initialize partial product,result array to the zero */

for(i=0;i<8;i++)
{
m1[i]=0;
ans[i]=0;
}

for(i=3;i>=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<<m1[j];
cout<<"\n";
}
c=0;  /* to store carry */
for(j=7;j>=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<<""<<ans[i];
}
getch();
}```
SDKING Do keep the above points in mind
and,.. Happy Coding👍👍
@SDKING • 29 Mar, 2013 thanx....guys...
@Sanyam Khurana • 29 Mar, 2013 Agree with rahul69
@simplycoder • 29 Mar, 2013 • 1 like
``` /**********************************************************************************************//***********************Multiplication of 4 bit binary Numbers.********************************//**********************************************************************************************//**********************************************************************************************/// Author    :  simplycoder// Date      :  29/03/2013// IDE      :  Code::Blocks 12.11/**********************************************************************************************/ ///Region of macros - start.#include<iostream>///Region of macros - end.using namespace std;///Region of Global variables - startconst 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=0; index<numberOfBitsInANumber; index++)    {        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=0; index<numberOfBitsInANumber; index++)    {        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=0; indexOne<numberOfBitsInANumber; indexOne++)    {        answerIndex=indexOne;        for(indexTwo=0; indexTwo<numberOfBitsInANumber; indexTwo++)        {            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-1; index>=0; index--)    {        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 • 30 Mar, 2013 Right guys.....thnx....
@SDKING • 02 Apr, 2013 Hey guy's my project is complete and working properly....
thanx to all of you for helping me....
8.2k views

## Related Posts

@Gaurav Deshmukh · Sep 19, 2013

hey guys , just write any tricy c code ... as the game everyone will answer it .. ok 1st my turn,,, what will be the output of following code...
27.5k views

@Ankita Katdare · Jun 3, 2015

There are many websites around that follow the model of FreeCharge. I have used the website many times and have received really good deals for recharging my phone through them....
9.6k views

@Chirayu Samarth · Jan 1, 2015

The Indian mobile manufacturer, Xolo has announced its first smartphone of the year- Q700 Club. The smartphone is listed on the company’s official website and is priced at INR 6,999....
9.7k views

@Guneet Khanuja · Feb 1, 2015

Girnar Software is an ITeS company co-founded by Amit Jain in the year 2007. Girnar Software works on offshore products and software development. The mission of GirnarSoft is to create...
5.3k views

@Kaustubh Katdare · May 9, 2009

At least two reliable sources have informed that Capgemini is firing employees in its India offices. Can someone confirm this?
8.8k views