Add two numbers with out use +

Write a function that adds two numbers. You should not use + or any arithmetic operations.

C++:
01 int addwithoutplus(int a, int b)
02 {
03     if(a==0)
04         return b;
05     if(b==0)
06         return a;
07     int sum=a^b;
08     int carry=(a&b)<<1;
09     return addwithoutplus(sum, carry);
10 }
Advertisements
This entry was posted in Bit operation, Number trick. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s