Like Tree3Likes

adding two binary numbers

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. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    28

    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;}
    
    }
    printf("\nThe answer is ");
    for(a=31;a>=0;a--)
    printf("%c", z[a]);
    }
    Last edited by renz15; 08-02-2011 at 06:30 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you know how to add them together if you had them written down on paper?

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,570
    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.
    renz15 likes this.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    @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. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    num1 = strtol( firststring, NULL, 2 );
    num2 = strtol( secondstring, NULL, 2 );
    num3 = num1 + num2;
    When in doubt, cheat using the standard library!


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Quote Originally Posted by quzah View Post
    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. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    How are you storing the binary numbers in your program? Is it a user inputted value?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Sorry for the double post. My internet connection is not stable as of now.
    Last edited by renz15; 08-01-2011 at 05:36 PM. Reason: double post

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Quote Originally Posted by AndrewHunter View Post
    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. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    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;}
    
    }
    printf("\nThe answer is ");
    for(a=31;a>=0;a--)
    printf("%c", z[a]);
    }
    Last edited by renz15; 08-02-2011 at 06:31 AM.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    ^
    please give me more hints.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by renz15 View Post
    ^
    please give me more hints.
    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. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    I have a question. How can I store every digit of the inputted binary number using array of ints?

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Numbers adding together = bad
    By Zyk0tiK in forum C Programming
    Replies: 5
    Last Post: 12-04-2005, 03:37 PM
  2. Adding big numbers
    By FoodDude in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 02:36 PM
  3. adding odd numbers only
    By CheyenneWay in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2004, 12:22 AM
  4. adding numbers
    By Allen_Buchannon in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 04:43 PM
  5. adding odd numbers
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 09-06-2001, 01:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21