Adding binary numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 21
Reputation: JaksLax is an unknown quantity at this point 
Solved Threads: 0
JaksLax's Avatar
JaksLax JaksLax is offline Offline
Newbie Poster

Adding binary numbers

 
0
  #1
Feb 23rd, 2007
I have this program I have to write about adding binary numbers. Here is the write up:

"Defining a binary number as int binNum[8]; write a C++ function
void binaryAdd(int* sum, int& cBit, const int* bin1, const int* bin2)
to compute sum as the sum of the two binary numbers, bin1 and bin2. cBit should be the value of the carry bit after the addition. sum should be the result in binary. For example, if the two binary numbers are 01111111 and 00111111, then sum will be 10111110 and carry will be 0. Test your function with interactive input."


Now I know how to go about adding the binary numbers, and the carry bit part I can figure out. What I am having troube with is how the binaryAdd function is taking in the sum and cBit. I must be looking at this completely wrong because to me it looks like you pass the sum and cBit into the function, but I though that is what the function is suppose to produce. I am confused with pointers and references, I dont really understand why they are being used in this situation. Any help starting this off would be appreciated. Im so lost. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Adding binary numbers

 
0
  #2
Feb 23rd, 2007
Let's see some more code please. You don't have to use pointers.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 21
Reputation: JaksLax is an unknown quantity at this point 
Solved Threads: 0
JaksLax's Avatar
JaksLax JaksLax is offline Offline
Newbie Poster

Re: Adding binary numbers

 
0
  #3
Feb 23rd, 2007
Yes I do have to use pointers, thats part of the assignment. I dont know how to even start the function with the way the assignment is written, so I am asking for help on how the pointers work and how I can go about gettign started.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Adding binary numbers

 
0
  #4
Feb 23rd, 2007
Originally Posted by JaksLax View Post
I must be looking at this completely wrong because to me it looks like you pass the sum and cBit into the function, but I though that is what the function is suppose to produce.
sum and cbit are values returned back to the calling function. Your function will load these values with the answers.

Originally Posted by JaksLax View Post
I am confused with pointers and references, I dont really understand why they are being used in this situation.
Because the instructor said so.

Pointers are simply another way to access an array. Read up on them and ask specific questions about them and we can help you. This link has some information about pointers vs. arrays.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Adding binary numbers

 
0
  #5
Feb 23rd, 2007
> because to me it looks like you pass the sum and cBit into the function
Well one is a pointer, and the other is a reference. So they could both be used for input, input/output or output.

The other two parameters are declared const, so that just makes them inputs.

  1. int a[8] = { 0, 0, 0, 0, 0, 0, 0, 1 };
  2. int b[8] = { 0, 0, 0, 0, 0, 0, 0, 1 };
  3. int result[8];
  4. int carry = 0;
  5. binaryAdd( result, carry, a, b);
  6. // now print out result and carry
  7. // a and b will not be changed
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 21
Reputation: JaksLax is an unknown quantity at this point 
Solved Threads: 0
JaksLax's Avatar
JaksLax JaksLax is offline Offline
Newbie Poster

Re: Adding binary numbers

 
0
  #6
Feb 24th, 2007
Thank you for the help starting off and the links to help out. You guys rock.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC