31 Jul 2014

Add Numbers

Problem Statement:
         Add two given numbers without '+' operator.

Download Source Code
Solution:
      We will be adding two given numbers using 'AND' and 'XOR' logical operators.

Fig: Half Adder , S: Sum and C: Carry

Sum of two number can be achieved with Half adder, if carry is greater than 0 , then it the process is repeated again. This is half adder implementation.

and as per full adder implementation :
   Sum = 2*C + S

Below are the different ways of getting the sum using XOR and AND logical operators.

Method 1 : 

int add(int a, int b) {
  while (b != 0) {
   int carry = a & b;
   a = a ^ b;
   b = carry << 1;
  }
  return a;
 }




Method 2:


 int add(int a, int b) {
  if (b == 0)
   return a;
  else
   return add2(a ^ b, (a & b) << 1);
 }



Method 3:


  int add(int a,int b){
  int carry= a & b;
  int sum= a ^ b;
  
  carry = 2* carry;
  
  sum = sum ^ carry;
  
  return sum;
 }


Method 4:


 int add(int a,int b){
  return ( 2*( a & b) ) ^ (a ^ b);
 }

Please post your comment and suggestions.
Happy Coding !! :)


References: 
Wikipedia

22 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. First off I would like to say great blog! I had a quick question in which I'd
    like to ask if you do not mind. I was interestted to know how you center yourself and clear your thoughts before writing.
    I've had a hard time clearing my thoughts in getting my thoughts out.
    I do take pleasure in writing however it just seems like the first 10 to 15 minutes tend to
    be lost just trying to figure outt how to begin. Any recommendations or tips?
    Cheers!

    Here is my homepage Las Vegas Whirlpool Repairman

    ReplyDelete
  3. My relatives all the time say that I am wasting my time
    here at web, except I know I am getting know-how everyday by reading thes pleasant
    articles or reviews.

    Take a look at my website - online etiquette

    ReplyDelete
  4. Do you mind if I quote a few of your articles as long as
    I provide credit and sources back to your website?
    My blog site is in the exact same niche as yours
    and my visitors would definitely benefit from some of the information you present here.

    Please let me know if this alright with you. Cheers!

    Look into my page; boeing business jet price

    ReplyDelete
  5. Wow! After all I got a website from where I be capable of genuinely get
    useful facts regarding my study and knowledge.

    Also visit my web blog - ทีฟาว

    ReplyDelete
  6. Hi there to all, because I am actually eager of reading this
    web site's post to be updated regularly. It carries
    good stuff.

    My blog post ... right seo company

    ReplyDelete
  7. Useful info. Lucky me I discovered your site byy
    chance,and I'm surprised why this coincidence didn't cam about
    earlier! I bookmarked it.

    Feel free to surf to my homepage ... Las Vegas Microwave Repairman

    ReplyDelete
  8. I'm really loving the theme/design of your website.

    Do you ever run into any internet browser compatibility issues?
    A handful of my blog visitors have complained about my
    website not working correctly in Explorer but looks great in Chrome.
    Do you have any solutions to help fix this issue?

    My blog - medical negligence solicitors

    ReplyDelete
  9. Hey there, I think your website might be having browser compatibility issues.
    When I look at your blog site in Firefox, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up!
    Other then that, great blog!

    my webpage ... possible search results

    ReplyDelete
  10. Right here is the perfect site for anybody who would like to find out about this topic.
    You realize a whole lot its almost hard to argue
    with you (not that I personally will need to…HaHa). You definitely put a new
    spin on a subject that's been discussed for many
    years. Wonderful stuff, just great!

    Look at my web blog; tefal standgrill

    ReplyDelete
  11. Quality articles or reviews is the key to interest the viewers
    to visit the site, that's what this site is providing.

    My page: cost to hire a private jet

    ReplyDelete
  12. What's Going down i am new to this, I stumbled upon this I've found It absolutely helpful and it has
    aided me out loads. I'm hoping to contribute & assist other customers like its aided me.
    Great job.

    Look at my page social media policies

    ReplyDelete
  13. My spouse and I absolutely love your blog and find a lot of your post's to be what
    precisely I'm looking for. Do you offer guest writers to write content available for you?
    I wouldn't mind writing a post or elaborating on a lot of the subjects you write with regards to
    here. Again, awesome website!

    Here is my web-site :: private jet hire london

    ReplyDelete
    Replies
    1. Hi ,
      Nice to hear that , ya sure i encourage you to write and i will post it on your behalf, you can write to me at appcrazyprateek@gmail.com

      Delete
  14. excellent issues altogether, you simply gained
    a new reader. What might you suggest in regards to
    your submit that you simply made a few days ago? Any certain?

    Take a look at my page: google search

    ReplyDelete
  15. Woah! I'm really digging the template/theme of
    this website. It's simple, yet effective. A lot of times it's tough
    to get that "perfect balance" between usability and visual appearance.
    I must say you've done a great job with this.
    Additionally, the blog loads very fast for me on Opera.
    Exceptional Blog!

    my page :: no win no fee medical claims

    ReplyDelete
  16. Ahaa, its pleasant dialogue regarding this paragraph at this place
    at this weblog, I have read all that, so now me also commenting here.


    Also visit my homepage :: tefal optigrill купить

    ReplyDelete
  17. Thanks a bunch for sharing this with all folks you actually realize what you are speaking approximately!
    Bookmarked. Please also seek advice from my site =). We could have a hyperlink change arrangement among us

    Here is my weblog :: website promotion

    ReplyDelete
  18. You ought to be a part of a contest for one of the finest blogs online.

    I will highly recommend this site!

    My web blog; mobile website

    ReplyDelete
  19. Very nice article, exactly what I was looking for.

    Feel free to surf to my web-site - digital agency in london

    ReplyDelete
  20. When someone writes an paragraph he/she maintains the thought of a user in his/her mind that how a user can know it.
    So that's why this piece of writing is perfect.
    Thanks!

    My homepage: clinical negligence london

    ReplyDelete