944,150 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2249
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 17th, 2006
0

Can someone please help me with Binary Addtion.

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  1.  
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include<algorithm>
  7. using namespace std;
  8.  
  9. void binConvert(int, vector<int>);//function prototype
  10. void binAddCal ( vector<int>,vector<int> );//function prototype
  11. int findMax(int, int);//function prototype
  12.  
  13. int main()
  14. {
  15. // int maxNumber, num=3;
  16. int userNum1, userNum2;
  17. vector<int> num1Vec;
  18. vector<int> num2Vec;
  19. vector<int> num1BinVec;
  20. vector<int> num2BinVec;
  21. vector <int> addBinVec;
  22.  
  23.  
  24.  
  25. cout<<"Please enter a base 10 number"<<endl;
  26. cin>> userNum1;
  27. cout<<"Please enter a base 10 number"<<endl;
  28. cin>> userNum2;
  29.  
  30. cout<<"The binary number for "<< userNum1 <<" is: "<<endl;
  31. binConvert(userNum1, num1Vec);
  32. cout<<endl;
  33. cout<<"The binary number for "<< userNum2 <<" is: "<<endl;
  34. binConvert(userNum2, num2Vec);
  35. cout<<endl;
  36.  
  37. cout<< "The result of adding the two base 10 numbers " <<userNum1<<" and "<<userNum2<< " is: "<<userNum1 + userNum2<<endl;
  38.  
  39. cout<<"The result of adding the two base 10 numbers in their binary form is: " <<endl;
  40. binAddCal (num1BinVec,num2BinVec);//To call add function if num1BinVec is bigger.
  41. // maxNumber= findMax(userNum1,userNum2);//to make sure bigger vector is taken as first parameter.
  42. // if (maxNumber ==userNum1)
  43. // {
  44. // binAddCal (num1BinVec,num2BinVec);//To call add function if num1BinVec is bigger.
  45. // }
  46. // else
  47. // binAddCal(userNum2,userNum1,num2BinVec,num1BinVec);//To call add function if num2BinVec is bigger.
  48.  
  49.  
  50. return 0;
  51. }
  52.  
  53.  
  54. void binConvert( int num1 , vector<int>vecNum)
  55. {
  56. vector<int> vecBinary;
  57. int num = 3; //intialize the num to set the - in the binary display.
  58. do
  59. {
  60. vecNum.push_back(num1); //To fill a vector to hold the results from the divsion of the base 10 numbers.
  61. num1 = num1 / 2;
  62. }while(num1 != 0);
  63.  
  64. for( int i = 0; i <vecNum.size(); i++) //A loop to go through the num1 answer vector and to get the remainder
  65. {
  66. vecBinary.push_back(vecNum[i] % 2); //To fill a vector with the binary number of the base number entered.
  67. }
  68.  
  69. if (vecBinary.size() < 8) //To check to see if the vector is small than 8 bits.
  70. {
  71. while(vecBinary.size() < 8) //To loop until vector becomes 8 bits.
  72. {
  73. vecBinary.push_back(0); //To add 0 to make it an 8 bit display.
  74. }
  75. }
  76. else
  77. {
  78. while ((vecBinary.size() / 8) ==1) //To loop while the vector is not in multiples of 8.
  79. {
  80. vecBinary.push_back(0); //To add 0 to make a multiple of 8 bit display.
  81. }
  82. }
  83.  
  84.  
  85.  
  86. //To reverse the vector so the binary number with display correctly.
  87. reverse(vecBinary.begin(), vecBinary.end());
  88. for(i = 0; i <vecBinary.size(); i++) //To loop through the num1 binary vector.
  89. {
  90. cout<< vecBinary[i]<< " "; //To display each element in the num1 binary vector.
  91.  
  92.  
  93. if( i == num && num + 1 < vecBinary.size()) //Decide if a - is needed.
  94. {
  95. cout<<"- "; //To place a - in the binary display for readablity.
  96. num+=4; //To increment by 4 so it shows up every 4 bits.
  97. }
  98.  
  99. }
  100.  
  101.  
  102.  
  103. return ;
  104.  
  105. }
  106. int findMax(int num1,int num2)
  107. {
  108.  
  109.  
  110. if (num1>num2)
  111. return num1;
  112. else
  113. return num2;
  114.  
  115.  
  116. return 0 ;
  117. }
  118.  
  119.  
  120. void binAddCal(vector<int>vec1,vector<int>vec2)
  121.  
  122. {
  123.  
  124.  
  125. vector<int>addBinVec;
  126. :
  127.  
  128.  
  129. int carry, num = 3;
  130.  
  131. for(int i = 0; i <vec1.size(); i++)
  132. {
  133.  
  134. if(vec1[i] == 1 && vec2[i]==1 && carry ==1)
  135. { addBinVec.push_back(1);
  136. carry = 0;
  137. }
  138. if(vec1[i] == 1 &&vec2[i]==1 && carry==0)
  139. { addBinVec.push_back(0);
  140. carry=1;
  141. }
  142. if(vec1[i] == 0 && vec2[i]==1&& carry==0)
  143. { addBinVec.push_back(1);
  144. carry=0;
  145. }
  146. if(vec1[i]== 0&& vec2[i]==1&& carry==1)
  147. { addBinVec.push_back(0);
  148. carry=1;
  149. }
  150. if(vec1[i]== 1&& vec2[i]==0 && carry==0)
  151. { addBinVec.push_back(1);
  152. carry=0;
  153. }
  154. if(vec1[i] == 1 && vec2[i]==0 && carry==1)
  155. { addBinVec.push_back(0);
  156. carry=1;
  157. }
  158. if(vec1[i]== 0&& vec2[i] && carry==0)
  159. { addBinVec.push_back(0);
  160. carry=0;
  161. }
  162. if(vec1[i] == 0 && vec2[i]==0 && carry==1)
  163. { addBinVec.push_back(1);
  164. carry=0;
  165. }
  166. }
  167.  
  168.  
  169.  
  170. for( i = 0; i <addBinVec.size(); i++) //To loop through the num1 binary vector.
  171. {
  172. cout<< addBinVec[i]<< " "; //To display each element in the num1 binary vector.
  173.  
  174.  
  175. if (addBinVec.size() < 8) //To check to see if the vector is small than 8 bits.
  176. {
  177. while(addBinVec.size() < 8) //To loop until vector becomes 8 bits.
  178. {
  179. addBinVec.push_back(0); //To add 0 to make it an 8 bit display.
  180. }
  181. }
  182. else
  183. {
  184. while ((addBinVec.size() / 8) ==1) //To loop while the vector is not in multiples of 8.
  185. {
  186. addBinVec.push_back(0); //To add 0 to make a multiple of 8 bit display.
  187. }
  188. }
  189. }
  190.  
  191. return ;
  192. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lulug76 is offline Offline
9 posts
since Apr 2005
Apr 18th, 2006
0

Re: Can someone please help me with Binary Addtion.

your code is too long, better if u mark in ur code the problem area.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Apr 18th, 2006
0

Re: Can someone please help me with Binary Addtion.

Ok sorry didn't realize how long my code was. I can fiqure out why my binAddCal function is not working it is the last funtion in the code. Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lulug76 is offline Offline
9 posts
since Apr 2005
Apr 18th, 2006
0

Re: Can someone please help me with Binary Addtion.

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 18th, 2006
0

Re: Can someone please help me with Binary Addtion.

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 a AND b bit-shifted one place left.

if the carrier evaluates to zero, the result of the calculation becomes a XOR b

if 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
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Apr 18th, 2006
0

Re: Can someone please help me with Binary Addtion.

Incidentally, C++ has an STL container called a bitset - it might be better to use that than a vector of int's or bool's or whatever. Bitsets are designed with binary operations in mind
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Apr 18th, 2006
0

Re: Can someone please help me with Binary Addtion.

Thanks for all of your input, I have to use vectors as part of the problem. My problem is I can't figure out why the vector isn't being passed to the function. I the vectors as agruments of the function but it keeps saying the size is zero and I don't know why.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lulug76 is offline Offline
9 posts
since Apr 2005
Apr 19th, 2006
0

Re: Can someone please help me with Binary Addtion.

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.

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include<algorithm>
  6. using namespace std;
  7.  
  8. void binConvert(int, vector<int> &);//function prototype
  9. void binAddCal ( vector<int>,vector<int> );//function prototype
  10. int findMax(int, int);//function prototype
  11.  
  12. int main()
  13. {
  14. // int maxNumber, num=3;
  15. int userNum1, userNum2;
  16. vector<int> num1Vec;
  17. vector<int> num2Vec;
  18. vector<int> num1BinVec;
  19. vector<int> num2BinVec;
  20. vector <int> addBinVec;
  21.  
  22.  
  23.  
  24. cout<<"Please enter a base 10 number"<<endl;
  25. cin>> userNum1;
  26. cout<<"Please enter a base 10 number"<<endl;
  27. cin>> userNum2;
  28.  
  29. cout<<"The binary number for "<< userNum1 <<" is: "<<endl;
  30. binConvert(userNum1, num1Vec);
  31. cout<<endl;
  32.  
  33. cout<<"\n****num1Vec****"<<endl;
  34. for(int i=0; i< (int)num1Vec.size();++i) {
  35. cout<<num1Vec[i] ;
  36. }
  37. cout<<"\n********"<<endl;
  38.  
  39. cout<<"The binary number for "<< userNum2 <<" is: "<<endl;
  40. binConvert(userNum2, num2Vec);
  41.  
  42. cout<<"\n*****num2Vec***"<<endl;
  43. for(i =0; i<(int)num2Vec.size();++i) {
  44. cout<<num2Vec[i] ;
  45. }
  46. cout<<"\n********"<<endl;
  47.  
  48. cout<<endl;
  49.  
  50. cout<< "The result of adding the two base 10 numbers " <<userNum1<<" and "<<userNum2<< " is: "<<userNum1 + userNum2<<endl;
  51.  
  52. cout<<"The result of adding the two base 10 numbers in their binary form is: " <<endl;
  53. binAddCal (num1Vec,num2Vec);//To call add function if num1BinVec is bigger.
  54. // maxNumber= findMax(userNum1,userNum2);//to make sure bigger vector is taken as first parameter.
  55. // if (maxNumber ==userNum1)
  56. // {
  57. // binAddCal (num1BinVec,num2BinVec);//To call add function if num1BinVec is bigger.
  58. // }
  59. // else
  60. // binAddCal(userNum2,userNum1,num2BinVec,num1BinVec);//To call add function if num2BinVec is bigger.
  61.  
  62.  
  63. return 0;
  64. }
  65.  
  66.  
  67. void binConvert( int num1 , vector<int>& vecNum)
  68. {
  69. vector<int> vecBinary;
  70. int num = 3; //intialize the num to set the - in the binary display.
  71. do
  72. {
  73. vecNum.push_back(num1); //To fill a vector to hold the results from the divsion of the base 10 numbers.
  74. num1 = num1 / 2;
  75. }while(num1 != 0);
  76.  
  77. /*cout<<"\n****VVV****"<<endl;
  78. for(int kki =0; kki<(int)vecNum.size();++kki) {
  79. cout<<vecNum[kki] ;
  80. }
  81. cout<<"\n****VVV****"<<endl;*/
  82. for( int i = 0; i <vecNum.size(); i++) //A loop to go through the num1 answer vector and to get the remainder
  83. {
  84. vecBinary.push_back(vecNum[i] % 2); //To fill a vector with the binary number of the base number entered.
  85. }
  86.  
  87. if (vecBinary.size() < 8) //To check to see if the vector is small than 8 bits.
  88. {
  89. while(vecBinary.size() < 8) //To loop until vector becomes 8 bits.
  90. {
  91. vecBinary.push_back(0); //To add 0 to make it an 8 bit display.
  92. }
  93. }
  94. else
  95. {
  96. while ((vecBinary.size() / 8) ==1) //To loop while the vector is not in multiples of 8.
  97. {
  98. vecBinary.push_back(0); //To add 0 to make a multiple of 8 bit display.
  99. }
  100. }
  101.  
  102.  
  103.  
  104. //To reverse the vector so the binary number with display correctly.
  105. reverse(vecBinary.begin(), vecBinary.end());
  106. for(i = 0; i <vecBinary.size(); i++) //To loop through the num1 binary vector.
  107. {
  108. cout<< vecBinary[i]<< " "; //To display each element in the num1 binary vector.
  109.  
  110.  
  111. if( i == num && num + 1 < vecBinary.size()) //Decide if a - is needed.
  112. {
  113. cout<<"- "; //To place a - in the binary display for readablity.
  114. num+=4; //To increment by 4 so it shows up every 4 bits.
  115. }
  116.  
  117. }
  118. vecNum.clear() ;
  119. for( int kki =0; kki<(int)vecBinary.size();++kki) {
  120. vecNum.push_back(vecBinary[kki]) ;
  121. }
  122. //reverse(vecNum.begin(), vecNum.end());
  123. /*cout<<"\n****If Right****"<<endl;
  124. for( kki =0; kki<(int)vecBinary.size();++kki) {
  125. cout<<vecNum[kki] ;
  126. }
  127. cout<<"\n****VVVVVVV****"<<endl;*/
  128. return ;
  129.  
  130. }
  131. int findMax(int num1,int num2)
  132. {
  133.  
  134.  
  135. if (num1>num2)
  136. return num1;
  137. else
  138. return num2;
  139.  
  140.  
  141. return 0 ;
  142. }
  143.  
  144.  
  145. void binAddCal(vector<int>vec1,vector<int>vec2)
  146.  
  147. {
  148.  
  149.  
  150. vector<int>addBinVec;
  151.  
  152.  
  153. int carry = 0, num = 3;
  154. int i=0;
  155.  
  156. if(vec1[0] == 1 && vec2[0]==1) {
  157. addBinVec.push_back(0);
  158. carry = 1;
  159. }
  160. else if(vec1[0] == 0 && vec2[0]==0) {
  161. addBinVec.push_back(0);
  162. carry = 0;
  163. }
  164. else {
  165. addBinVec.push_back(1);
  166. carry = 0;
  167. }
  168. for( i = 1; i <vec1.size(); i++)
  169. {
  170.  
  171. if(vec1[i] == 1 && vec2[i]==1 && carry ==1)
  172. { addBinVec.push_back(1);
  173. carry = 0;
  174. }
  175. if(vec1[i] == 1 &&vec2[i]==1 && carry==0)
  176. { addBinVec.push_back(0);
  177. carry=1;
  178. }
  179. if(vec1[i] == 0 && vec2[i]==1&& carry==0)
  180. { addBinVec.push_back(1);
  181. carry=0;
  182. }
  183. if(vec1[i]== 0&& vec2[i]==1&& carry==1)
  184. { addBinVec.push_back(0);
  185. carry=1;
  186. }
  187. if(vec1[i]== 1&& vec2[i]==0 && carry==0)
  188. { addBinVec.push_back(1);
  189. carry=0;
  190. }
  191. if(vec1[i] == 1 && vec2[i]==0 && carry==1)
  192. { addBinVec.push_back(0);
  193. carry=1;
  194. }
  195. if(vec1[i]== 0&& vec2[i]==1&& carry==0)
  196. { addBinVec.push_back(0);
  197. carry=0;
  198. }
  199. if(vec1[i] == 0 && vec2[i]==0 && carry==1)
  200. { addBinVec.push_back(1);
  201. carry=0;
  202. }
  203. }
  204.  
  205.  
  206.  
  207. reverse(addBinVec.begin(), addBinVec.end());
  208. //for( i = 0; i <addBinVec.size(); i++) //To loop through the num1 binary vector.
  209. for( i = 0; i < addBinVec.size(); i++) //To loop through the num1 binary vector.
  210. {
  211. cout<< addBinVec[i]<< " "; //To display each element in the num1 binary vector.
  212.  
  213.  
  214. //if (addBinVec.size() < 8) //To check to see if the vector is small than 8 bits.
  215. // {
  216. // while(addBinVec.size() < 8) //To loop until vector becomes 8 bits.
  217. // {
  218. // addBinVec.push_back(0); //To add 0 to make it an 8 bit display.
  219. // }
  220. // }
  221. //else
  222. // {
  223. // while ((addBinVec.size() / 8) ==1) //To loop while the vector is not in multiples of 8.
  224. // {
  225. // addBinVec.push_back(0); //To add 0 to make a multiple of 8 bit display.
  226. // }
  227. // }
  228. }
  229. return ;
  230. }
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Apr 19th, 2006
0

Re: Can someone please help me with Binary Addtion.

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]
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 19th, 2006
0

Re: Can someone please help me with Binary Addtion.

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
C++ Syntax (Toggle Plain Text)
  1. int binaryAdd(int a, int b)
  2. {
  3. return (a&b)? binaryAdd( ( (a&b) << 1 ) , a^b ) : a^b;
  4. }
You could emulate this function using STL containers - you'd need to emulate the bitwise operators used: left-shift, XOR and AND.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: tic tac toe help.
Next Thread in C++ Forum Timeline: Function pointers inside classes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC