Given two integers, add their binary representation.

For example,

**Input: **

x = 12731 (which is 0011000110111011 in binary)

y = 38023 (which is 1001010010000111 in binary)

**Output: **1100011001000010

**Input: **

x = 1023 (which is 0000001111111111 in binary)

y = 1023 (which is 0000001111111111 in binary)

**Output: **0000011111111110

Binary addition is much like decimal addition, except that it carries on a value of 2 instead of a value of 10. For example in decimal addition, if we add 9 + 6 we get 16 which in the sum gives a digit 6 and a carry of 1. Binary addition follows the similar logic.

- when we add 1 and 1 (with carry of 0); the result is 2 in decimal, but since 2 is written as 10 in binary, we get, after summing 1 + 1 in binary, a digit 0 and a carry of 1.

- Similarly, if we add 1 and 1 (with carry of 1), the result in 3 in decimal which is 11 in binary. So after summing 1 + 1 + 1 in binary, we get a digit 1 and a carry of 1.

Therefore in binary,

0 + 0 = 0

0 + 1 = 1

1 + 0 = 1

1 + 1 = 10 (which is 0 with carry 1)

**with carry 1**

0 + 0 (+ 1) = 1

0 + 1 (+ 1) = 10 (which is 0 with carry 1)

1 + 0 (+ 1) = 10 (which is 0 with carry 1)

1 + 1 (+ 1) = 11 (which is 1 with carry 1)

So the idea is to extract each bit from both integers one by one from LSB to MSB and store their addition result in an array of size of integer. If addition results in a carry, we propagate the carry to next pair of bits. Below implementation uses LEFT shift and AND binary operator.

## C++

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#include <bits/stdc++.h> using namespace std; // macro that defines the size of an integer // assuming 2 byte storage for integer #define SIZE 16 // Function to add binary representation of x and y int* add(int x, int y) { int carry = 0; int n = SIZE; // create a array to store binary sum int* arr = new int[n]; for (int i = 0; i < n; i++) { // if x is 1 if (x & (1 << i)) { // if y is 1 if (y & (1 << i)) // if both x and y are 1 { if (carry) arr[n - i - 1] = 1; // carry = 1 else arr[n - i - 1] = 0, carry = 1; } else // x is 1, y is 0 { if (carry) arr[n - i - 1] = 0; // carry = 1 else arr[n - i - 1] = 1; // carry = 0 } } // if x is 0 else { if (y & (1 << i)) // x is 0, y is 1 { if (carry) arr[n - i - 1] = 0; // carry = 1 else arr[n - i - 1] = 1; // carry = 0 } else // both x and y are 0 { if (carry == 1) arr[n - i - 1] = 1, carry = 0; else arr[n - i - 1] = 0; // carry = 0 } } } return arr; } // main function int main() { int x = 12731, y = 38023; cout << "x (" << x << ") in binary is " << bitset<16>(x) << endl; cout << "y (" << y << ") in binary is " << bitset<16>(y) << endl; int* arr = add(x, y); cout << "\nx + y is "; for (unsigned i = 0; i < SIZE; i++) printf("%d", arr[i]); delete[] arr; return 0; } |

`Output:`

x (12731) in binary is 0011000110111011

y (38023) in binary is 1001010010000111

x + y is 1100011001000010

Check similar implementation using RIGHT shift and AND binary operator here.

**Thanks for reading.**

Please use our online compiler to post code in comments. To contribute, get in touch with us.

Like us? Please spread the word and help us grow. Happy coding 🙂

## Leave a Reply

Thanks!