| | |
Can someone please help me with Binary Addtion.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2005
Posts: 9
Reputation:
Solved Threads: 0
Hi,
I am working on a project that I had to get 2 number from a user display them in 10base. Then convert them into 2 base and display them in multiple of 8. I did this part. The next part I am suppose to do is add and subtract them using binary addition and subtraction(2 compliment for subtraction. I have written some code but I don't know why my addition function isn't doing anything. I am not getting any errors but I can't seem to pass the vectors to the function. Well thanks here's the code I have.
I am working on a project that I had to get 2 number from a user display them in 10base. Then convert them into 2 base and display them in multiple of 8. I did this part. The next part I am suppose to do is add and subtract them using binary addition and subtraction(2 compliment for subtraction. I have written some code but I don't know why my addition function isn't doing anything. I am not getting any errors but I can't seem to pass the vectors to the function. Well thanks here's the code I have.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> #include <string> #include<algorithm> using namespace std; void binConvert(int, vector<int>);//function prototype void binAddCal ( vector<int>,vector<int> );//function prototype int findMax(int, int);//function prototype int main() { // int maxNumber, num=3; int userNum1, userNum2; vector<int> num1Vec; vector<int> num2Vec; vector<int> num1BinVec; vector<int> num2BinVec; vector <int> addBinVec; cout<<"Please enter a base 10 number"<<endl; cin>> userNum1; cout<<"Please enter a base 10 number"<<endl; cin>> userNum2; cout<<"The binary number for "<< userNum1 <<" is: "<<endl; binConvert(userNum1, num1Vec); cout<<endl; cout<<"The binary number for "<< userNum2 <<" is: "<<endl; binConvert(userNum2, num2Vec); cout<<endl; cout<< "The result of adding the two base 10 numbers " <<userNum1<<" and "<<userNum2<< " is: "<<userNum1 + userNum2<<endl; cout<<"The result of adding the two base 10 numbers in their binary form is: " <<endl; binAddCal (num1BinVec,num2BinVec);//To call add function if num1BinVec is bigger. // maxNumber= findMax(userNum1,userNum2);//to make sure bigger vector is taken as first parameter. // if (maxNumber ==userNum1) // { // binAddCal (num1BinVec,num2BinVec);//To call add function if num1BinVec is bigger. // } // else // binAddCal(userNum2,userNum1,num2BinVec,num1BinVec);//To call add function if num2BinVec is bigger. return 0; } void binConvert( int num1 , vector<int>vecNum) { vector<int> vecBinary; int num = 3; //intialize the num to set the - in the binary display. do { vecNum.push_back(num1); //To fill a vector to hold the results from the divsion of the base 10 numbers. num1 = num1 / 2; }while(num1 != 0); for( int i = 0; i <vecNum.size(); i++) //A loop to go through the num1 answer vector and to get the remainder { vecBinary.push_back(vecNum[i] % 2); //To fill a vector with the binary number of the base number entered. } if (vecBinary.size() < 8) //To check to see if the vector is small than 8 bits. { while(vecBinary.size() < 8) //To loop until vector becomes 8 bits. { vecBinary.push_back(0); //To add 0 to make it an 8 bit display. } } else { while ((vecBinary.size() / 8) ==1) //To loop while the vector is not in multiples of 8. { vecBinary.push_back(0); //To add 0 to make a multiple of 8 bit display. } } //To reverse the vector so the binary number with display correctly. reverse(vecBinary.begin(), vecBinary.end()); for(i = 0; i <vecBinary.size(); i++) //To loop through the num1 binary vector. { cout<< vecBinary[i]<< " "; //To display each element in the num1 binary vector. if( i == num && num + 1 < vecBinary.size()) //Decide if a - is needed. { cout<<"- "; //To place a - in the binary display for readablity. num+=4; //To increment by 4 so it shows up every 4 bits. } } return ; } int findMax(int num1,int num2) { if (num1>num2) return num1; else return num2; return 0 ; } void binAddCal(vector<int>vec1,vector<int>vec2) { vector<int>addBinVec; : int carry, num = 3; for(int i = 0; i <vec1.size(); i++) { if(vec1[i] == 1 && vec2[i]==1 && carry ==1) { addBinVec.push_back(1); carry = 0; } if(vec1[i] == 1 &&vec2[i]==1 && carry==0) { addBinVec.push_back(0); carry=1; } if(vec1[i] == 0 && vec2[i]==1&& carry==0) { addBinVec.push_back(1); carry=0; } if(vec1[i]== 0&& vec2[i]==1&& carry==1) { addBinVec.push_back(0); carry=1; } if(vec1[i]== 1&& vec2[i]==0 && carry==0) { addBinVec.push_back(1); carry=0; } if(vec1[i] == 1 && vec2[i]==0 && carry==1) { addBinVec.push_back(0); carry=1; } if(vec1[i]== 0&& vec2[i] && carry==0) { addBinVec.push_back(0); carry=0; } if(vec1[i] == 0 && vec2[i]==0 && carry==1) { addBinVec.push_back(1); carry=0; } } for( i = 0; i <addBinVec.size(); i++) //To loop through the num1 binary vector. { cout<< addBinVec[i]<< " "; //To display each element in the num1 binary vector. if (addBinVec.size() < 8) //To check to see if the vector is small than 8 bits. { while(addBinVec.size() < 8) //To loop until vector becomes 8 bits. { addBinVec.push_back(0); //To add 0 to make it an 8 bit display. } } else { while ((addBinVec.size() / 8) ==1) //To loop while the vector is not in multiples of 8. { addBinVec.push_back(0); //To add 0 to make a multiple of 8 bit display. } } } return ; }
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
I asssume you meant you can't fiqure out why my binAddCal function is not working. Assuming that's the case, here's how I'd
Given:
Usually we write numerical values with largest placeholder to the left and lowest to the right. However, that is convention only. It could be the other way around just as easily.
And:
One of the more common ways to represent binary numbers is using an array of digits restricted in value to 0 or 1.
Then:
To add binary numbers you could first reverse the usual order so the lowest placeholder has index 0, the second has index 1, etc. Then to add two binary numbers begin with a carry value of zero and add the two elements with index of zero and the carry value together. If the modulus of the sum is zero then the value of the element at index zero of the binary sum is zero, too, else it is one. If the sum is greater than one then the carry value to 1 else it is zero and go to the next set if indexes. Repeat the proces by looping through all elements of the shortest array. Then add the last carry value to the next element of the longer array. Treat the sum as you did before and repeat until this process until the sum is less than 2 or the last element of the longer array had been used. If the last element of longer array has been found and the carry is 1 then put one in the next element of the binary sum array. Now if you reverse the binary sum array you have the binary number is the usual left largest placeholder value form again.
There are probably other ways to do it too, but this seems most logical to me at this point.
Given:
Usually we write numerical values with largest placeholder to the left and lowest to the right. However, that is convention only. It could be the other way around just as easily.
And:
One of the more common ways to represent binary numbers is using an array of digits restricted in value to 0 or 1.
Then:
To add binary numbers you could first reverse the usual order so the lowest placeholder has index 0, the second has index 1, etc. Then to add two binary numbers begin with a carry value of zero and add the two elements with index of zero and the carry value together. If the modulus of the sum is zero then the value of the element at index zero of the binary sum is zero, too, else it is one. If the sum is greater than one then the carry value to 1 else it is zero and go to the next set if indexes. Repeat the proces by looping through all elements of the shortest array. Then add the last carry value to the next element of the longer array. Treat the sum as you did before and repeat until this process until the sum is less than 2 or the last element of the longer array had been used. If the last element of longer array has been found and the carry is 1 then put one in the next element of the binary sum array. Now if you reverse the binary sum array you have the binary number is the usual left largest placeholder value form again.
There are probably other ways to do it too, but this seems most logical to me at this point.
This sort of boolean arithmetic is probably simplest with a recursive algorithm.
for the integers, a and b..
Take a temporary 'carrier' number which records the result of
if the carrier evaluates to zero, the result of the calculation becomes
if the carrier is nonzero (meaning, some bits needed to be carried), call the recursive function using the result, and the carrier.
for the integers, a and b..
Take a temporary 'carrier' number which records the result of
a AND b bit-shifted one place left.if the carrier evaluates to zero, the result of the calculation becomes
a XOR bif the carrier is nonzero (meaning, some bits needed to be carried), call the recursive function using the result, and the carrier.
Last edited by Bench; Apr 18th, 2006 at 9:38 pm. Reason: mistake
hi
try wid this code, i was nt heaving much time to correct it for addition of more thn 8 bit numbers. it should work fine for less thn 8 bit addition.
try wid this code, i was nt heaving much time to correct it for addition of more thn 8 bit numbers. it should work fine for less thn 8 bit addition.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> #include <string> #include<algorithm> using namespace std; void binConvert(int, vector<int> &);//function prototype void binAddCal ( vector<int>,vector<int> );//function prototype int findMax(int, int);//function prototype int main() { // int maxNumber, num=3; int userNum1, userNum2; vector<int> num1Vec; vector<int> num2Vec; vector<int> num1BinVec; vector<int> num2BinVec; vector <int> addBinVec; cout<<"Please enter a base 10 number"<<endl; cin>> userNum1; cout<<"Please enter a base 10 number"<<endl; cin>> userNum2; cout<<"The binary number for "<< userNum1 <<" is: "<<endl; binConvert(userNum1, num1Vec); cout<<endl; cout<<"\n****num1Vec****"<<endl; for(int i=0; i< (int)num1Vec.size();++i) { cout<<num1Vec[i] ; } cout<<"\n********"<<endl; cout<<"The binary number for "<< userNum2 <<" is: "<<endl; binConvert(userNum2, num2Vec); cout<<"\n*****num2Vec***"<<endl; for(i =0; i<(int)num2Vec.size();++i) { cout<<num2Vec[i] ; } cout<<"\n********"<<endl; cout<<endl; cout<< "The result of adding the two base 10 numbers " <<userNum1<<" and "<<userNum2<< " is: "<<userNum1 + userNum2<<endl; cout<<"The result of adding the two base 10 numbers in their binary form is: " <<endl; binAddCal (num1Vec,num2Vec);//To call add function if num1BinVec is bigger. // maxNumber= findMax(userNum1,userNum2);//to make sure bigger vector is taken as first parameter. // if (maxNumber ==userNum1) // { // binAddCal (num1BinVec,num2BinVec);//To call add function if num1BinVec is bigger. // } // else // binAddCal(userNum2,userNum1,num2BinVec,num1BinVec);//To call add function if num2BinVec is bigger. return 0; } void binConvert( int num1 , vector<int>& vecNum) { vector<int> vecBinary; int num = 3; //intialize the num to set the - in the binary display. do { vecNum.push_back(num1); //To fill a vector to hold the results from the divsion of the base 10 numbers. num1 = num1 / 2; }while(num1 != 0); /*cout<<"\n****VVV****"<<endl; for(int kki =0; kki<(int)vecNum.size();++kki) { cout<<vecNum[kki] ; } cout<<"\n****VVV****"<<endl;*/ for( int i = 0; i <vecNum.size(); i++) //A loop to go through the num1 answer vector and to get the remainder { vecBinary.push_back(vecNum[i] % 2); //To fill a vector with the binary number of the base number entered. } if (vecBinary.size() < 8) //To check to see if the vector is small than 8 bits. { while(vecBinary.size() < 8) //To loop until vector becomes 8 bits. { vecBinary.push_back(0); //To add 0 to make it an 8 bit display. } } else { while ((vecBinary.size() / 8) ==1) //To loop while the vector is not in multiples of 8. { vecBinary.push_back(0); //To add 0 to make a multiple of 8 bit display. } } //To reverse the vector so the binary number with display correctly. reverse(vecBinary.begin(), vecBinary.end()); for(i = 0; i <vecBinary.size(); i++) //To loop through the num1 binary vector. { cout<< vecBinary[i]<< " "; //To display each element in the num1 binary vector. if( i == num && num + 1 < vecBinary.size()) //Decide if a - is needed. { cout<<"- "; //To place a - in the binary display for readablity. num+=4; //To increment by 4 so it shows up every 4 bits. } } vecNum.clear() ; for( int kki =0; kki<(int)vecBinary.size();++kki) { vecNum.push_back(vecBinary[kki]) ; } //reverse(vecNum.begin(), vecNum.end()); /*cout<<"\n****If Right****"<<endl; for( kki =0; kki<(int)vecBinary.size();++kki) { cout<<vecNum[kki] ; } cout<<"\n****VVVVVVV****"<<endl;*/ return ; } int findMax(int num1,int num2) { if (num1>num2) return num1; else return num2; return 0 ; } void binAddCal(vector<int>vec1,vector<int>vec2) { vector<int>addBinVec; int carry = 0, num = 3; int i=0; if(vec1[0] == 1 && vec2[0]==1) { addBinVec.push_back(0); carry = 1; } else if(vec1[0] == 0 && vec2[0]==0) { addBinVec.push_back(0); carry = 0; } else { addBinVec.push_back(1); carry = 0; } for( i = 1; i <vec1.size(); i++) { if(vec1[i] == 1 && vec2[i]==1 && carry ==1) { addBinVec.push_back(1); carry = 0; } if(vec1[i] == 1 &&vec2[i]==1 && carry==0) { addBinVec.push_back(0); carry=1; } if(vec1[i] == 0 && vec2[i]==1&& carry==0) { addBinVec.push_back(1); carry=0; } if(vec1[i]== 0&& vec2[i]==1&& carry==1) { addBinVec.push_back(0); carry=1; } if(vec1[i]== 1&& vec2[i]==0 && carry==0) { addBinVec.push_back(1); carry=0; } if(vec1[i] == 1 && vec2[i]==0 && carry==1) { addBinVec.push_back(0); carry=1; } if(vec1[i]== 0&& vec2[i]==1&& carry==0) { addBinVec.push_back(0); carry=0; } if(vec1[i] == 0 && vec2[i]==0 && carry==1) { addBinVec.push_back(1); carry=0; } } reverse(addBinVec.begin(), addBinVec.end()); //for( i = 0; i <addBinVec.size(); i++) //To loop through the num1 binary vector. for( i = 0; i < addBinVec.size(); i++) //To loop through the num1 binary vector. { cout<< addBinVec[i]<< " "; //To display each element in the num1 binary vector. //if (addBinVec.size() < 8) //To check to see if the vector is small than 8 bits. // { // while(addBinVec.size() < 8) //To loop until vector becomes 8 bits. // { // addBinVec.push_back(0); //To add 0 to make it an 8 bit display. // } // } //else // { // while ((addBinVec.size() / 8) ==1) //To loop while the vector is not in multiples of 8. // { // addBinVec.push_back(0); //To add 0 to make a multiple of 8 bit display. // } // } } return ; }
er that's wrong.
I tried it with 23 and 34...didn't work.
Tee he he. Try again
http://img476.imageshack.us/img476/5171/cut20ln.png
Piworld ™
[Tis simple as Pie]
I tried it with 23 and 34...didn't work.
Tee he he. Try again
http://img476.imageshack.us/img476/5171/cut20ln.png
Piworld ™
[Tis simple as Pie]
If you have to use vectors for this (Wouldn't be my first choice, a deque would to allow you to use push_front - eliminating the annoying step of reversing the bit order), rather than vector<int> - you are only holding 1s and 0s, so use vector<bool> - this could simplify your code a fair bit.
You could also reduce alot of repeated code if you used a seperate container for your carrier, rather than a single variable.
this may solve one of the problems with your binAddCal() function, that you are using the carry variable without initialising it. (result - undefined behaviour)
As I mentioned in the other post, there is a much slimmer way to do this using bitwise operators in a recursive function You could emulate this function using STL containers - you'd need to emulate the bitwise operators used: left-shift, XOR and AND.
You could also reduce alot of repeated code if you used a seperate container for your carrier, rather than a single variable.
this may solve one of the problems with your binAddCal() function, that you are using the carry variable without initialising it. (result - undefined behaviour)
As I mentioned in the other post, there is a much slimmer way to do this using bitwise operators in a recursive function
C++ Syntax (Toggle Plain Text)
int binaryAdd(int a, int b) { return (a&b)? binaryAdd( ( (a&b) << 1 ) , a^b ) : a^b; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: tic tac toe help.
- Next Thread: Function pointers inside classes
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






