# Bitwise recursive addition of two integers

When adding two binary numbers by hand we keep the carry bits in mind and add it at the same time. But to do same thing in program we need a lot of checks. Recursive solution can be imagined as addition of *carry* and *a^b* (two inputs) until *carry* becomes 0.

**Examples :**

Input : int x = 45, y = 45 Output : 90 Input : int x = 4, y = 78 Output : 82

Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits.

Above is simple Half Adder logic that can be used to add 2 single bits. We can extend this logic for integers. If x and y don’t have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result.

One important observation is, if (x & y) becomes 0, then result is x ^ y.

## C

`// CPP program to do recursive addition ` `// of two integers ` `#include <stdio.h> ` ` ` `int` `add(` `int` `x, ` `int` `y) { ` ` ` `int` `keep = (x & y) << 1; ` ` ` `int` `res = x^y; ` ` ` ` ` `// If bitwise & is 0, then there ` ` ` `// is not going to be any carry. ` ` ` `// Hence result of XOR is addition. ` ` ` `if` `(keep == 0) ` ` ` `return` `res; ` ` ` ` ` `add(keep, res); ` `} ` ` ` `// Driver code ` `int` `main(){ ` ` ` `printf` `(` `"%d"` `, add(15, 38)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to do recursive addition ` `// of two integers ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `add(` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `int` `keep = (x & y) << ` `1` `; ` ` ` `int` `res = x^y; ` ` ` ` ` `// If bitwise & is 0, then there ` ` ` `// is not going to be any carry. ` ` ` `// Hence result of XOR is addition. ` ` ` `if` `(keep == ` `0` `) ` ` ` `return` `res; ` ` ` ` ` `return` `add(keep, res); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `System.out.println(add(` `15` `, ` `38` `)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ajit. ` |

*chevron_right*

*filter_none*

## Python3

` ` `# Python program to do recursive addition ` `# of two integers ` ` ` `def` `add(x, y): ` ` ` `keep ` `=` `(x & y) << ` `1` `; ` ` ` `res ` `=` `x^y; ` ` ` ` ` `# If bitwise & is 0, then there ` ` ` `# is not going to be any carry. ` ` ` `# Hence result of XOR is addition. ` ` ` `if` `(keep ` `=` `=` `0` `): ` ` ` `return` `res; ` ` ` ` ` `return` `add(keep, res); ` ` ` ` ` `# Driver code ` ` ` `print` `(add(` `15` `, ` `38` `)); ` ` ` `# This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# program to do recursive ` `// addition of two integers ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `add(` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `int` `keep = (x & y) << 1; ` ` ` `int` `res = x^y; ` ` ` ` ` `// If bitwise & is 0, then there ` ` ` `// is not going to be any carry. ` ` ` `// Hence result of XOR is addition. ` ` ` `if` `(keep == 0) ` ` ` `return` `res; ` ` ` ` ` `return` `add(keep, res); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `Console.Write(add(15, 38)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Smitha. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// php program to do recursive addition ` `// of two integers ` ` ` `function` `add(` `$x` `, ` `$y` `) { ` ` ` `$keep` `= (` `$x` `& ` `$y` `) << 1; ` ` ` `$res` `= ` `$x` `^` `$y` `; ` ` ` ` ` `// If bitwise & is 0, then there ` ` ` `// is not going to be any carry. ` ` ` `// Hence result of XOR is addition. ` ` ` `if` `(` `$keep` `== 0) ` ` ` `{ ` ` ` `echo` `$res` `; ` ` ` `exit` `(0); ` ` ` `} ` ` ` ` ` `add(` `$keep` `, ` `$res` `); ` `} ` ` ` `// Driver code ` `$k` `= add(15, 38); ` ` ` `// This code is contributed by mits. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

53

## Recommended Posts:

- Recursive program to print formula for GCD of n integers
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Addition of two numbers without carry
- Recursive Functions
- Recursive Insertion Sort
- Modular exponentiation (Recursive)
- Recursive Implementation of atoi()
- Recursive Bubble Sort
- Recursive Program for Binary to Decimal
- Recursive program for prime number
- Recursive Practice Problems with Solutions
- Recursive program to print all subsets with given sum
- Recursive program to generate power set

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.