This is a discussion on adding two binary numbers within the C Programming forums, part of the General Programming Boards category; hello guys~ I'm asked to make a program that will add two 32 bit binary numbers. I don't have any ...

1. ## adding two binary numbers

hello guys~
I'm asked to make a program that will add two 32 bit binary numbers. I don't have any idea on what to do so please give me some tips and ideas.

This is what I've done so far and I still can't get the correct answer when I add two binary numbers. Please tell me what to do.

Code:
```# include<stdio.h>
#include <string.h>
char x[32];
char y[32];
char z[32];
int a;
int carry=0;
main()
{
printf("Please input the first binary number.\n");
scanf("%s", x);
printf("Please input the second binary number.\n");
scanf("%s", y);
for(a=0;a<=31;a++){
if (x[a]=='0' && y[a]=='0' && carry==0){
z[a]='0';
carry=0;}
if (x[a]=='1' && y[a]=='0' && carry==0){
z[a]='1';
carry=0;}
if (x[a]=='0' && y[a]=='1' && carry==0){
z[a]='1';
carry=0;}
if (x[a]=='1' && y[a]=='1' && carry==0){
z[a]='0';
carry=1;}
if (x[a]=='1' && y[a]=='1' && carry==1){
z[a]='1';
carry=1;}
if (x[a]=='0' && y[a]=='0' && carry==1){
z[a]='1';
carry=0;}
if (x[a]=='0' && y[a]=='1' && carry==1){
z[a]='0';
carry=1;}
if (x[a]=='1' && y[a]=='0' && carry==1){
z[a]='0';
carry=1;}

}
for(a=31;a>=0;a--)
printf("%c", z[a]);
}```

2. Do you know how to add them together if you had them written down on paper?

3. I'd recommend first reviewing the homework policy of this site.
Then try to make as much of the program as you're able to. When you get stuck, then you can ask specific questions.
Follow these tips and you'll have a much better chance of getting meaningful assistance on this board.

4. @tabstop of course i can

My problem is how to scan every digit of the inputted number. That's why I can't move on to the addition part. Can you give some ideas on how to do it using arrays?

5. Code:
```num1 = strtol( firststring, NULL, 2 );
num2 = strtol( secondstring, NULL, 2 );
num3 = num1 + num2;```
When in doubt, cheat using the standard library!

Quzah.

6. Originally Posted by quzah
Code:
```num1 = strtol( firststring, NULL, 2 );
num2 = strtol( secondstring, NULL, 2 );
num3 = num1 + num2;```
When in doubt, cheat using the standard library!

Quzah.
sorry for the noob question. what does that do? I am not supposed to use functions (i don't know the term) in my program.

7. How are you storing the binary numbers in your program? Is it a user inputted value?

8. Sorry for the double post. My internet connection is not stable as of now.

9. Originally Posted by AndrewHunter
How are you storing the binary numbers in your program? Is it a user inputted value?
Yes. And my problem is how scan and store every digit using arrays.

10. That's what scanf is for, right? Are you trying to scan as characters or ints? Once you know that, that tells you how to write the format for scanf.

(And to save the "I don't know where to put the first number until I read them all in" complaint, you should read the whole line of input (which will tell you how many characters were read), and then use sscanf to read out of the string.)

11. This is what I've done so far and I still can't get the correct answer when I add two binary numbers. Please tell me what to do.

Code:
```# include<stdio.h>
#include <string.h>
char x[32];
char y[32];
char z[32];
int a;
int carry=0;
main()
{
printf("Please input the first binary number.\n");
scanf("%s", x);
printf("Please input the second binary number.\n");
scanf("%s", y);
for(a=0;a<=31;a++){
if (x[a]=='0' && y[a]=='0' && carry==0){
z[a]='0';
carry=0;}
if (x[a]=='1' && y[a]=='0' && carry==0){
z[a]='1';
carry=0;}
if (x[a]=='0' && y[a]=='1' && carry==0){
z[a]='1';
carry=0;}
if (x[a]=='1' && y[a]=='1' && carry==0){
z[a]='0';
carry=1;}
if (x[a]=='1' && y[a]=='1' && carry==1){
z[a]='1';
carry=1;}
if (x[a]=='0' && y[a]=='0' && carry==1){
z[a]='1';
carry=0;}
if (x[a]=='0' && y[a]=='1' && carry==1){
z[a]='0';
carry=1;}
if (x[a]=='1' && y[a]=='0' && carry==1){
z[a]='0';
carry=1;}

}
for(a=31;a>=0;a--)
printf("%c", z[a]);
}```

12. Apparently you know about loops.... think about how to do this in a loop.

(hint: 1 digit or 1000 digits, it's all the same thing)

13. ^

14. Originally Posted by renz15
^
I already did... two big ones.

Now your job is to sit down with pencil and paper and work out the problem...

Programming happens in 4 steps...
1) Understand the problem.
2) Plan a solution.
3) Write Code.
4) Test and debug your solution.

You are still on step 1.

15. I have a question. How can I store every digit of the inputted binary number using array of ints?

Page 1 of 4 1234 Last