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.

Member Avatar for iamthwee

Let's see some more code please. You don't have to use pointers.

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.

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.

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.

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

int a[8] = { 0, 0, 0, 0, 0, 0, 0, 1 };
int b[8] = { 0, 0, 0, 0, 0, 0, 0, 1 };
int result[8];
int carry = 0;
binaryAdd( result, carry, a, b);
// now print out result and carry
// a and b will not be changed

Thank you for the help starting off and the links to help out. You guys rock.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.