1,105,221 Community Members

Binary addition in C

Member Avatar
Jammie
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

Hi there,

I tried binary addition. It seems to work fine. Could someone tell me if there's a better way of doin this or any bugs that could be uncovered in the code below?

I appreciate your time.

Thanks,
Tina

#include <stdio.h>

void binaryAdd(char *, char *);

int main()
{
	char op1[20], op2[20];
	char sum[20];
	char *p, *p1;

	fgets(op1, sizeof(op1), stdin);
	fgets(op2, sizeof(op2), stdin);

    if( (p = strchr(op1, '\n')) != NULL)
		*p = '\0';

    if( (p1 = strchr(op2, '\n')) != NULL)
	    *p1 = '\0';

	binaryAdd(op1, op2);

	return 0;
}

void binaryAdd(char *first, char *second)
{
	int carry = 0;
    int flen = strlen(first);
	int slen = strlen(second);

    char *tfirst = first + strlen(first) - 1;
	char *tsecond = second + strlen(second) - 1;

	int *result = malloc(sizeof(int) * 16);
    int *temp = result;


	for(; flen > 0 || slen > 0; flen--, slen--)
	{
        if((*tfirst - '0') ^ (*tsecond - '0'))
		{
			if(!carry)
			{
				*temp++ = 1 + carry;  
				carry = 0;
			}
			else
				*temp++ = 0;
		}
		else
		{
			if((*tfirst - '0') & (*tsecond -'0'))
			{
				*temp++ = (0 + carry);
				carry = 1;
			}
			else
			{
				*temp++ = (0 + carry);
				carry = 0;
			}
		}
	
		tfirst--;
		tsecond--;
	}

	if (carry == 1)
	{
		*temp++ = carry;
	}
	
    while(temp-- != result)
		printf("%d", *temp);

	free(result);
}
Member Avatar
jephthah
Posting Maven
2,572 posts since Feb 2008
Reputation Points: 1,888 [?]
Q&As Helped to Solve: 196 [?]
Skill Endorsements: 5 [?]
 
1
 

why so complicated? all you really need is "strtol()" and the addition operator.

int binaryAdd(char *first, char *second, int *sum)
{
    int val1, val2;
    char *ptr1, *ptr2;
    size_t length;

    val1 = strtol(first,&ptr1,2);
    val2 = strtol(second,&ptr2,2);

    if (ptr1 == first || ptr2 == second)
    {
        printf("\ninvalid entry.\n\n");
        return 0;
    }

    length = ptr1-first;
    if ((ptr2-second) > length)
        length = ptr2-second;

    *sum = val1+val2;

    printf("\n  %*s (%d)\n+ %*s (%d)\n\n= %*s (%d)\n\n",
        length, first, val1, length, second, val2, length, " ", *sum);

    return 1;
}

(The rest is just decoration)

I'll leave conversion of the numeric sum, back into a binary string, as an exercise to the reader :P


.

Member Avatar
Jammie
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

Thanks for responding!

I was thinking of doing it at the bit level using the bit operators.

Thanks again,
Tina

Member Avatar
jephthah
Posting Maven
2,572 posts since Feb 2008
Reputation Points: 1,888 [?]
Q&As Helped to Solve: 196 [?]
Skill Endorsements: 5 [?]
 
0
 

at the "bit level", every integer is inherently binary and hex and decimal... it does not matter, because there's only a difference when you print it.

so, you can make it hard if you like, just for the sake of having a challenge or if your instructor requires you to do so for learning purposes.

but if you're not required to make it difficult, i'd say use the <stdlib.h> standard library functions whenever possible.

Member Avatar
monkey_king
Junior Poster
160 posts since Aug 2008
Reputation Points: 39 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

Well, this isn't at the bitlevel,

you are interpreting the ascii values '0' '1' as bit 0 and bit 1.

If you are interested in this bitlevel stuff,
you should look into the shift operations. like

unsigned char x=7;
unsigned char y=x<<1;
//y is now 7 times 2

But it looks nice your code,
one thing though, you don't need

if (carry == 1)
    *temp++ = carry;
//just
if(carray)

A good exercise is to write you addition program with a non fixed base like,


digit_n = (digit_{n-1}\textrm{ div }base) + (digit_n\textrm{ mod }base)

Member Avatar
viwenka
Newbie Poster
3 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

why so complicated? all you really need is "strtol()" and the addition operator.

int binaryAdd(char *first, char *second, int *sum)
{
    int val1, val2;
    char *ptr1, *ptr2;
    size_t length;

    val1 = strtol(first,&ptr1,2);
    val2 = strtol(second,&ptr2,2);

    if (ptr1 == first || ptr2 == second)
    {
        printf("\ninvalid entry.\n\n");
        return 0;
    }

    length = ptr1-first;
    if ((ptr2-second) > length)
        length = ptr2-second;

    *sum = val1+val2;

    printf("\n  %*s (%d)\n+ %*s (%d)\n\n= %*s (%d)\n\n",
        length, first, val1, length, second, val2, length, " ", *sum);

    return 1;
}

(The rest is just decoration)

I'll leave conversion of the numeric sum, back into a binary string, as an exercise to the reader :P


.

Can u pls explain me where's input: printf("Enter 1st ...
("Enter 2nd binary ...
and output: printf("Result=... ?

i'm looking for a binary addition, without using library, just stdio.h

Member Avatar
arunsolo1984
Newbie Poster
12 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

how to perform 64 bit addition in c with code and explanation?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article