Binary addition in C

Reply

Join Date: Oct 2007
Posts: 6
Reputation: Jammie is an unknown quantity at this point 
Solved Threads: 0
Jammie Jammie is offline Offline
Newbie Poster

Binary addition in C

 
1
  #1
May 28th, 2009
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

  1. #include <stdio.h>
  2.  
  3. void binaryAdd(char *, char *);
  4.  
  5. int main()
  6. {
  7. char op1[20], op2[20];
  8. char sum[20];
  9. char *p, *p1;
  10.  
  11. fgets(op1, sizeof(op1), stdin);
  12. fgets(op2, sizeof(op2), stdin);
  13.  
  14. if( (p = strchr(op1, '\n')) != NULL)
  15. *p = '\0';
  16.  
  17. if( (p1 = strchr(op2, '\n')) != NULL)
  18. *p1 = '\0';
  19.  
  20. binaryAdd(op1, op2);
  21.  
  22. return 0;
  23. }
  24.  
  25. void binaryAdd(char *first, char *second)
  26. {
  27. int carry = 0;
  28. int flen = strlen(first);
  29. int slen = strlen(second);
  30.  
  31. char *tfirst = first + strlen(first) - 1;
  32. char *tsecond = second + strlen(second) - 1;
  33.  
  34. int *result = malloc(sizeof(int) * 16);
  35. int *temp = result;
  36.  
  37.  
  38. for(; flen > 0 || slen > 0; flen--, slen--)
  39. {
  40. if((*tfirst - '0') ^ (*tsecond - '0'))
  41. {
  42. if(!carry)
  43. {
  44. *temp++ = 1 + carry;
  45. carry = 0;
  46. }
  47. else
  48. *temp++ = 0;
  49. }
  50. else
  51. {
  52. if((*tfirst - '0') & (*tsecond -'0'))
  53. {
  54. *temp++ = (0 + carry);
  55. carry = 1;
  56. }
  57. else
  58. {
  59. *temp++ = (0 + carry);
  60. carry = 0;
  61. }
  62. }
  63.  
  64. tfirst--;
  65. tsecond--;
  66. }
  67.  
  68. if (carry == 1)
  69. {
  70. *temp++ = carry;
  71. }
  72.  
  73. while(temp-- != result)
  74. printf("%d", *temp);
  75.  
  76. free(result);
  77. }
Last edited by Ancient Dragon; May 28th, 2009 at 11:49 pm. Reason: corrected code tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Binary addition in C

 
1
  #2
May 28th, 2009
why so complicated? all you really need is "strtol()" and the addition operator.

  1. int binaryAdd(char *first, char *second, int *sum)
  2. {
  3. int val1, val2;
  4. char *ptr1, *ptr2;
  5. size_t length;
  6.  
  7. val1 = strtol(first,&ptr1,2);
  8. val2 = strtol(second,&ptr2,2);
  9.  
  10. if (ptr1 == first || ptr2 == second)
  11. {
  12. printf("\ninvalid entry.\n\n");
  13. return 0;
  14. }
  15.  
  16. length = ptr1-first;
  17. if ((ptr2-second) > length)
  18. length = ptr2-second;
  19.  
  20. *sum = val1+val2;
  21.  
  22. printf("\n %*s (%d)\n+ %*s (%d)\n\n= %*s (%d)\n\n",
  23. length, first, val1, length, second, val2, length, " ", *sum);
  24.  
  25. return 1;
  26. }

(The rest is just decoration)

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


.
Last edited by jephthah; May 29th, 2009 at 12:00 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: Jammie is an unknown quantity at this point 
Solved Threads: 0
Jammie Jammie is offline Offline
Newbie Poster

Re: Binary addition in C

 
0
  #3
May 29th, 2009
Hi,

Thanks for responding!

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

Thanks again,
Tina
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Binary addition in C

 
0
  #4
May 29th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: Binary addition in C

 
0
  #5
May 29th, 2009
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

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

But it looks nice your code,
one thing though, you don't need
  1. if (carry == 1)
  2. *temp++ = carry;
  3. //just
  4. if(carray)

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

<br />
digit_n = (digit_{n-1}\textrm{ div }base) + (digit_n\textrm{ mod }base)<br />
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC