473,385 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Binary adder problem


Hello Everyone,

I am trying to add two binary numbers in the form of two arrays. The
code that i have been using compiles, but it does not give me the
results i would expect. The problem that i am having is that it does
not carry ones over to the next significant digit if i have a 1 + 1 =
10 case. Could anyone help me by giving an explanation in the form of a
correction if possible. I would appreciate any feedback. Thank You !

Here is the code

// Binary number manipulation

#include
#include
#include
//#include

main()
{

int array_value1[ 4 ] = { 0, 0, 1, 1 }; //4 element array

int array_value2[ 4 ] = { 0, 1, 0, 1 }; //4 element array

int array_value3[4];

int i;
for( i = 0; i <4; i++)
// printf("d[%i] = %i \n", i, array_value1[i] );

for( i= 0; i <4; i++)
// printf("d[%i] = %i \n", i, array_value2[i]);

for (i=0; i <4; i++)
{
array_value3[i] = array_value1[i] + array_value2[i];

if ( (array_value1[i]== 1) && (array_value2[i]== 1))
{

array_value3[i] = 0;
array_value3[i+1] = 1 + array_value1[i+1] + array_value2[i+1];

}

}
for( i= 0; i <4; ++i)
printf("d[%i] = %i \n", i, array_value3[i]);

return(0);
}

--
LaVic
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Jul 3 '06 #1
6 1520
Hi,
I am trying to add two binary numbers in the form of two arrays. The
code that i have been using compiles, but it does not give me the
results i would expect. The problem that i am having is that it does
not carry ones over to the next significant digit if i have a 1 + 1 =
10 case. Could anyone help me by giving an explanation in the form of a
correction if possible. I would appreciate any feedback. Thank You !

Here is the code

// Binary number manipulation

#include
#include
#include
//#include

main()
{

int array_value1[ 4 ] = { 0, 0, 1, 1 }; //4 element array

int array_value2[ 4 ] = { 0, 1, 0, 1 }; //4 element array

int array_value3[4];

int i;
for( i = 0; i <4; i++)
// printf("d[%i] = %i \n", i, array_value1[i] );

for( i= 0; i <4; i++)
// printf("d[%i] = %i \n", i, array_value2[i]);

for (i=0; i <4; i++)
{
array_value3[i] = array_value1[i] + array_value2[i];

if ( (array_value1[i]== 1) && (array_value2[i]== 1))
{

array_value3[i] = 0;
your code doesn't care if there was a carry in the previous loop cycle.
array_value3[i] contains the carry of the previous iteration. if you
overwrite it,it is gone.
you could solve this by using a separate carry flag and using it properly.
array_value3[i+1] = 1 + array_value1[i+1] + array_value2[i+1];
this is dangerous. array_value3[i+1] will adress an element out of bounds if
there is an overflow in the final iteration.
again, you can solve this by using a separate carry flag to signal an
overflow.
>
}

}
for( i= 0; i <4; ++i)
printf("d[%i] = %i \n", i, array_value3[i]);

return(0);
}
--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Jul 3 '06 #2

LaVic schrieb:
I am trying to add two binary numbers in the form of two arrays.
This seems like an awful waste. The following is a little example of
how to do it using bit-wise operations on unsigned ints. The program
adds two positive integers. It could be refined in a lot of ways. It
seems to work correctly, but no guarantees. I tried to get it to show
that overflow occurred if the sum went out of range, but it doesn't
work, and I don't have time to work on it anymore. I hope you find it
helpful.

Laurence Finston

**************************************

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{

char c[8];

unsigned int mask = 1;
unsigned long carry_mask = 1; /* Wasting 31 bits here, but what the
heck. */

unsigned int operand_1 = 0;
unsigned int operand_2 = 0;
unsigned int result = 0;

unsigned short uint_size = sizeof(unsigned int) * 8; /* Assume 8
bits per byte
(a pretty
safe assumption). */

unsigned short curr_pos = 0;

while (true)
{

mask = 1;
carry_mask = 0;
result = 0;

#if 0
unsigned int max_mask = ~carry_mask;
cout << "max_mask == " << max_mask << endl;
#endif

cout << "Enter operand 1: ";
cin >operand_1;

cout << "Enter operand 2: ";
cin >operand_2;

for (curr_pos = 0; curr_pos < uint_size; ++curr_pos)
{

cout << "At beginning of loop:\n"
<< "curr_pos == " << curr_pos
<< endl
<< "mask == " << mask
<< endl
<< "carry_mask == " << carry_mask
<< endl;

if (mask & carry_mask & operand_1 & operand_2)
{
carry_mask <<= 1;
result |= mask;
}

else if (mask & carry_mask & (operand_1 | operand_2))
{
carry_mask <<=1;
}

else if (mask & carry_mask)
{
carry_mask = 0;
result |= mask;

}

else if (mask & ~carry_mask & operand_1 & operand_2)
{
carry_mask = mask << 1;

}
else if (mask & ~carry_mask & (operand_1 | operand_2))
{
carry_mask = 0;
result |= mask;
}

cout << "At end of loop:"
<< endl
<< "result == " << result << endl
<< "carry_mask == " << carry_mask
<< endl
<< "***************"
<< endl << endl;

mask <<= 1;

} /* |for| */

cout << "result == " << result << endl << endl;
cout << "carry_mask == " << carry_mask << endl;

/* This doesn't work. Don't have time to play with it anymore.
*/

if (carry_mask)
cout << "Overflow occurred.";
else
cout << "No overflow occurred.";

cout << "Enter `x' to quit, or any other character to continue:
";

cin >c;

if (!strcmp(c, "x"))
break;

} /* |while (true)| */

return 0;
}

Jul 7 '06 #3

lfins...@gwdg.de schrieb:
unsigned long carry_mask = 1; /* Wasting 31 bits here, but what the
heck. */
Of course, right away I saw something that I'd overlooked. There's no
reason to initialize `carry_mask' to 1.

Laurence

Jul 7 '06 #4

lfins...@gwdg.de schrieb:

Of course, all of the cases where `mask' and `carry_mask' are both
non-null have been accounted for, so ` & ~carry_mask' isn't needed.
>
else if (mask & ~carry_mask & operand_1 & operand_2)
{
carry_mask = mask << 1;

}
else if (mask & ~carry_mask & (operand_1 | operand_2))
{
carry_mask = 0;
result |= mask;
}
Laurence Finston

Jul 7 '06 #5
Here's a corrected version. I was assuming that `unsigned long' was
larger than `unsigned int'. This is not the case. It's possible to
keep track of possible overflow by shifting `carry_mask' at the
beginning of the loop. This way, the 1 in the left column, if present,
isn't shifted off the end during the final iteration.

Laurence Finston
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{

char c[8];

unsigned int mask = 1;
unsigned int carry_mask = 0;

unsigned int operand_1 = 0;
unsigned int operand_2 = 0;
unsigned int result = 0;

unsigned short uint_size = sizeof(unsigned int) * 8; /* Assume 8
bits per byte
(a pretty
safe assumption). */

while (true)
{

mask = 1;
carry_mask = 0;
result = 0;

#if 0

/* There's no need to calculate `max_mask', since it's the same
as the constant `UINT_MAX'. */

unsigned int max_mask = ~carry_mask;
cout << "max_mask == " << max_mask << endl
<< "UINT_MAX == " << UINT_MAX << endl;
// 4294967295

#endif

cout << "Enter operand 1: ";
cin >operand_1;

cout << "Enter operand 2: ";
cin >operand_2;

unsigned short curr_pos = 0;

for (curr_pos = 0; curr_pos < uint_size; ++curr_pos)
{

carry_mask <<= 1;

cout << "At beginning of loop:\n"
<< "curr_pos == " << curr_pos
<< endl
<< "mask == " << mask
<< endl
<< "carry_mask == " << carry_mask
<< endl;

if (mask & carry_mask & operand_1 & operand_2)
{
result |= mask;

}

else if (mask & carry_mask & (operand_1 | operand_2))
{

// Do nothing.
}

else if (mask & carry_mask)
{
carry_mask = 0;
result |= mask;
}

else if (mask & ~carry_mask & operand_1 & operand_2)
{
carry_mask = mask;
}
else if (mask & ~carry_mask & (operand_1 | operand_2))
{
carry_mask = 0;
result |= mask;
}

cout << "At end of loop:"
<< endl
<< "result == " << result << endl
<< "carry_mask == " << carry_mask
<< endl
<< "***************"
<< endl << endl;

mask <<= 1;

} /* |for| */

cout << "result == " << result << endl << endl;

cout << "carry_mask == " << carry_mask << endl;

if (carry_mask)
cout << "Overflow occurred." << endl;
else
cout << "No overflow occurred." << endl;
cout << "Enter `x' to quit, or any other character to continue:
";

cin >c;

if (!strcmp(c, "x"))
break;

} /* |while (true)| */

return 0;
}

Jul 10 '06 #6
Just for the sake of completeness, here's the same code, but with the
redundant
"& ~carry_mask" conditions removed.

Laurence Finston
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{

char c[8];

unsigned int mask = 1;
unsigned int carry_mask = 0;

unsigned int operand_1 = 0;
unsigned int operand_2 = 0;
unsigned int result = 0;

unsigned short uint_size = sizeof(unsigned int) * 8; /* Assume 8
bits per byte
(a pretty
safe assumption). */

while (true)
{

mask = 1;
carry_mask = 0;
result = 0;

#if 1
unsigned int max_mask = ~carry_mask;
cout << "max_mask == " << max_mask << endl
<< "UINT_MAX == " << UINT_MAX << endl;
// 4294967295

#endif

cout << "Enter operand 1: ";
cin >operand_1;

cout << "Enter operand 2: ";
cin >operand_2;

unsigned short curr_pos = 0;

for (curr_pos = 0; curr_pos < uint_size; ++curr_pos)
{

carry_mask <<= 1;

cout << "At beginning of loop:\n"
<< "curr_pos == " << curr_pos
<< endl
<< "mask == " << mask
<< endl
<< "carry_mask == " << carry_mask
<< endl;

if (mask & carry_mask & operand_1 & operand_2)
{
result |= mask;

}

else if (mask & carry_mask & (operand_1 | operand_2))
{

// Do nothing.
}

else if (mask & carry_mask)
{
carry_mask = 0;
result |= mask;
}

else if (mask & operand_1 & operand_2)
{
carry_mask = mask;
}
else if (mask & (operand_1 | operand_2))
{
carry_mask = 0;
result |= mask;
}

cout << "At end of loop:"
<< endl
<< "result == " << result << endl
<< "carry_mask == " << carry_mask
<< endl
<< "***************"
<< endl << endl;

mask <<= 1;

} /* |for| */

cout << "result == " << result << endl << endl;
cout << "carry_mask == " << carry_mask << endl;

if (carry_mask)
cout << "Overflow occurred." << endl;
else
cout << "No overflow occurred." << endl;
cout << "Enter `x' to quit, or any other character to continue:
";

cin >c;

if (!strcmp(c, "x"))
break;

} /* |while (true)| */

return 0;
}

Jul 10 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: christopher diggins | last post by:
Welcome to the first installment of the Diggins Public Domain Posts (PDP). There is a significant dearth of good public domain C++ code. All of it seems to come with one kind of a license or...
13
by: kerbbb | last post by:
Hello I am trying to add a long to long and I need some help example: 01101011 00001000 -------- ???????? does anyone have any idea how to make a simple function(a,b) I figure out that if I...
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
3
by: John R. Delaney | last post by:
I am running in debugging mode after a clean C++ compilation under .NET 2003. In a BIG loop (controlled many levels up in the call stack), I open a file with fopen using the "a" option. Then I write...
4
by: knapak | last post by:
Hello I'm a self instructed amateur attempting to read a huge file from disk... so bear with me please... I just learned that reading a file in binary is faster than text. So I wrote the...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
3
by: Seymour | last post by:
I created a module with the DictAdder subclass as follows: class DictAdder(Adder): def add(self, x, y): new={} for k in x.keys(): new=x for k in y.keys(): new=y return new At the...
3
by: nguser3552 | last post by:
Hello Everyone, I have a problem I can't surmount, anything is gravy at this point. I need to be able to read any type of file .ext (mov,mpeg,mp3,etc) in binary format. I can do this in C, but ...
3
by: Nehil | last post by:
How can we count no of ones in a binary no. plz don't tell by linear search. if any effecient logic u have in mind...plz tell me. Thanks in advance.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.