944,111 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 10058
  • C++ RSS
Feb 23rd, 2007
0

Adding binary numbers

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JaksLax is offline Offline
21 posts
since Sep 2006
Feb 23rd, 2007
0

Re: Adding binary numbers

Let's see some more code please. You don't have to use pointers.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 23rd, 2007
0

Re: Adding binary numbers

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JaksLax is offline Offline
21 posts
since Sep 2006
Feb 23rd, 2007
0

Re: Adding binary numbers

Click to Expand / Collapse  Quote originally posted by JaksLax ...
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.

Click to Expand / Collapse  Quote originally posted by JaksLax ...
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.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Feb 23rd, 2007
0

Re: Adding binary numbers

> 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.

C++ Syntax (Toggle Plain Text)
  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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Feb 24th, 2007
0

Re: Adding binary numbers

Thank you for the help starting off and the links to help out. You guys rock.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JaksLax is offline Offline
21 posts
since Sep 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: using Graphic.h
Next Thread in C++ Forum Timeline: Pls help :Find first non-repeated char in a string





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


Follow us on Twitter


© 2011 DaniWeb® LLC