What's problem of adding two binary number?

This is a discussion on What's problem of adding two binary number? within the C Programming forums, part of the General Programming Boards category; The problem appears in [url]http://acm.hnu.cn:8080/online/?action=problem&type=show&id=10475[\url] Just very simple method, first I reverse the binary number. Then I fill the zero ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    What's problem of adding two binary number?

    The problem appears in [url]http://acm.hnu.cn:8080/online/?action=problem&type=show&id=10475[\url]
    Just very simple method, first I reverse the binary number. Then I fill the zero in the remaining space. For example, a = 1001011111, b = 11110001.
    Code:
    1) reverse
    1001011111 ---> 1111101001
    11110001 ---> 10001111
    2) fill zero
    10001111 ---> 1000111100
    3) adding two binary number
    1111101001
    1000111100
    --------------
    1101010000
    Here is my code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAXDIGIT 100 
    
    char a[MAXDIGIT], b[MAXDIGIT];
    int n;
    
    void reverse(char *x)
    {
    	char tmp;
    	int i, len = strlen(x);
    	for (i = 0; i < len/2; i++) {
    		tmp = x[i];
    		x[i] = x[len-i-1];
    		x[len-i-1] = tmp;
    	}
    }
    
    void fillZero(char *a, char *b)
    {
    	int i;
    	if (strlen(a) > strlen(b)) {
    		for (i = strlen(b); i < strlen(a); i++)
    			b[i] = '0';
    		i++;
    		b[i] = '\0';
    	} else if (strlen(a) < strlen(b)) {
    		for (i = strlen(a); i < strlen(b); i++)
    			a[i] = '0';
    		i++;
    		a[i] = '\0';
    	}
    }
    
    int chr2int(char x)
    { return (int)x-'0'; }
    
    void base2Plus()
    {
    	int i, tmp, len;
    	int result[MAXDIGIT];
    	reverse(a);
    	reverse(b);
    	fillZero(a, b);
    	tmp = 0;
    	for (i = 0, len = 0; i < strlen(a); i++, len++) {
    		tmp += chr2int(a[i])+chr2int(b[i]);
    		result[len] = tmp%2;
    		tmp = (tmp <= 1) ? 0 : 1;
    	}
    	if (tmp == 1) {
    		result[len] = tmp%2;
    		len++;
    	}
    	for (i = len-1; i >= 0; i--)
    		printf("%d", result[i]);
    	printf("\n");
    }
    
    int main()
    {
    	int i;
    	scanf("%d", &n);
    	for (i = 1; i <= n; i++) {
    		scanf("%s %s", a, b);
    		printf("%d ", i);
    		base2Plus();
    	}
    	return 0;
    }
    But I cannot pass the task of the mentioned problem. What are the bugs here?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Well doing i++ before a[i] = '\0'; is wrong.

    Print out the strings before and after reverse() and fillZero() to make sure they're correct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Number theory problem
    By elad in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-02-2004, 10:02 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

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