-
How we can Multiply 4 bit binary Numers??Plz Any One Write The Source code for it...plz guys help me....0
-
Administrator • Mar 21, 2013
You're trying to do this, right? -SDKINGHow we can Multiply 4 bit binary Numers??Plz Any One Write The Source code for it...plz guys help me....
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?Are you sure? This action cannot be undone. -
Member • Mar 21, 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....Are you sure? This action cannot be undone. -
Member • Mar 25, 2013
did you try using debugger or print statements, step in through code step by step.SDKINGyes 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....
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.Are you sure? This action cannot be undone. -
Member • Mar 25, 2013
ok....Are you sure? This action cannot be undone. -
Member • Mar 26, 2013
hi...
@simplycoder-
this my code in Turbo c++ compiler...
/*code*/ #include<iostream.h> #include<conio.h> 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<<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; /*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<<""<<ans; } getch(); }
Are you sure? This action cannot be undone. -
Member • Mar 26, 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.Are you sure? This action cannot be undone. -
Member • Mar 28, 2013
How we can Multiply 4 bit binary Numers??Plz Any One Write The Source code for it...plz guys help me....Are you sure? This action cannot be undone. -
Member • Mar 28, 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[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<<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;
/*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<<""<<ans;
}
getch();
}Are you sure? This action cannot be undone. -
Member • Mar 28, 2013
wait buddy, let me understand your code,
BDW, you have posted in wrong section..!! 😛Are you sure? This action cannot be undone. -
Member • Mar 28, 2013
Sorry Buddy...my mistake...Are you sure? This action cannot be undone. -
Member • Mar 28, 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.Are you sure? This action cannot be undone. -
Member • Mar 28, 2013
realy...how???....can you explain plz??Are you sure? This action cannot be undone. -
Member • Mar 28, 2013
Check PM, and we could just PM everything😉
Dual posting is being done!!! LOLAre you sure? This action cannot be undone. -
Administrator • Mar 28, 2013
Posts moved to the most appropriate thread.Are you sure? This action cannot be undone. -
Member • Mar 28, 2013
haha, thank you Sir😀
#-Link-Snipped-# I hope now I have given you the right solution..👍
All the best..!!Are you sure? This action cannot be undone. -
Member • Mar 28, 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..Are you sure? This action cannot be undone. -
Member • Mar 28, 2013
If you have the algo, then what is the problem you are facing #-Link-Snipped-# ?Are you sure? This action cannot be undone. -
Member • Mar 28, 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...Are you sure? This action cannot be undone. -
Member • Mar 28, 2013
#-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++...👎Are you sure? This action cannot be undone. -
Member • Mar 28, 2013
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.SDKINGMy 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...
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.😀Are you sure? This action cannot be undone. -
Member • Mar 28, 2013
#include<iostream.h> #include<conio.h> 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<<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(); }
#-Link-Snipped-# Do keep the above points in mind
and,.. Happy Coding👍👍Are you sure? This action cannot be undone. -
Member • Mar 28, 2013
thanx....guys...Are you sure? This action cannot be undone. -
Member • Mar 28, 2013
Agree with #-Link-Snipped-#Are you sure? This action cannot be undone. -
Member • Mar 29, 2013
This is what I could come up with in 20 mins./**********************************************************************************************/
/***********************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 - 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=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;
}
There are few things which can be optimized, but due to constraints of time and readability, I haven't really optimized the code.Are you sure? This action cannot be undone. -
Member • Mar 29, 2013
Right guys.....thnx....Are you sure? This action cannot be undone. -
Member • Apr 1, 2013
Hey guy's my project is complete and working properly....
thanx to all of you for helping me....Are you sure? This action cannot be undone.