| | |
Adding binary numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
"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.
•
•
•
•
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.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
> 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.
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)
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
![]() |
Similar Threads
- Adding binary values from a file (C++)
- Adding two binary numbers and find over flow bits (C)
- Binary Numbers (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: using Graphic.h
- Next Thread: Pls help :Find first non-repeated char in a string
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






