Computing.Net > Forums > Programming > C++ program to add 2 bin. no's

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ program to add 2 bin. no's

Reply to Message Icon

Name: Nirav
Date: September 21, 2002 at 14:18:33 Pacific
OS: win98
CPU/Ram: PIII
Comment:

Hi,
please help me...
im trying to write a program that accepts 2 16-bit binary in 0's and 1's and adds the two no's and displays the result in binary.
2. wants to convert the two binary no's and the result into corresponding decimal no.

example: 0110000000000000
0110000000000000
------------------
1100000000000000

2. 24576
+ 24576
--------
-16384

im thinking the logic is correct and im not getting any errors but not adding and converting....kindly help me
Thank u
Nirav


#include
#include
#include
#include

void bin_add(char *, char *);
void char_2_dec(char *);

int main()
{
char num1[17], num2[17];
int i;

cout = 0; k--)
{
if(val1[k] != val2[k])
{
if(carry == 0)
result[k] = 1;
else
result[k] = 0;
}
else if((val1[k] == 0) && (val2[k] == 0))
{
if(carry == 0)
result[k] = 0;
else
result[k] = 1;

cout << " result: " << result[k] << endl;
}
else
{
if(carry == 0)
{
result[k] = 0;
carry = 1;
}
else
{
result[k] = 1;
carry = 1;
}
}
}
cout << " The Result is: " << result << endl;
}

void char_2_dec(char *val)
{
char dec [17];

strcpy(dec,val);

int max_index = 15, exp;
double sum = 0;

for(int k = 0; k <= max_index; k++)
{
exp = max_index - k;

cout << " exp is " << endl;
cout << " power is : " << pow(2,exp);
cout << " value " << dec[k] << endl;

if(dec[k] == 1)
sum = sum + pow(2,exp);
cout << " sum: " << sum << endl;
}

cout << " The Decimal value of " << dec << " is " << sum << endl;
}



Sponsored Link
Ads by Google

Response Number 1
Name: Sajid Mohammed
Date: September 22, 2002 at 12:51:51 Pacific
Reply:

Hi Nirav,
Your logic seems to be OK and it should work. In your post there are few lines missing. Check your posted message. If you can send the whole code without any missing lines then I can tell you exactly why its not working. You can email me the code if you prefer doing so or see that the code is correctly displayed in your next post.

Sajid Mohammed ( sajleo@yahoo.com )


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ program to add 2 bin. no's

converting a C program to dll file www.computing.net/answers/programming/converting-a-c-program-to-dll-file/12310.html

c++ program to download www.computing.net/answers/programming/c-program-to-download/5059.html

How to logon to Novell from 'C' program? www.computing.net/answers/programming/how-to-logon-to-novell-from-c-program/1099.html