-
Notifications
You must be signed in to change notification settings - Fork 5
/
GFG_SumWithoutArithmeticOperators.cpp
65 lines (57 loc) · 1.54 KB
/
GFG_SumWithoutArithmeticOperators.cpp
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
/* https://practice.geeksforgeeks.org/problems/sum-of-two-numbers-without-using-arithmetic-operators/1/#
* Given two integers a and b. Find the sum of two numbers without using arithmetic operators.
*/
// { Driver Code Starts
//Initial Template for C++
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution
{
public:
int sum(int a , int b)
{
//iterative approach of ++ -- won't work here
//using HALF ADDER APPROACH and Bit Manipulation
int sum, carry;
while(b!=0){
sum = a^b; // sum without carries
carry = a & b; //shows each location where carry is needed
a = sum;
b = carry<<1; // shift the carry left one column for adding
}//while loop
/*
* a = 10 = 00001010
* b = 30 = 00011110
*
* a = 00010100 (20)
* carry = 00001010 (10)
* b = carry<<1 = 00010100 (20)
*
* a = 00000000 (20)
* carry = 00010100 (20)
* b = carry<<1 = 00101000 (40)
*
* a = 00101000 (40)
* carry = 00000000 (0)
* b = carry<<1 = 00000000 (0)
*/
return a; // return final sum
}//sum
};
// { Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
int a,b;
cin >> a>>b;
Solution ob;
cout<< ob.sum(a,b) <<"\n";
}
return 0;
}
// } Driver Code Ends