Add two numbers without using arithmetic operators
How to add two number ?
now try this
but before doing this let me introduce background for this method.
so lets start..
a = 999
b = 909
let me tell you rule how to add it.
rule 1: Add them but do not add carry and store in variable sum
rule 2: Now add again but this time list out the carry’s and append 0 at end and store in list carry
rule 3: stop if list carry become 0 else move sum to a and list carry to b and repeat all this.
let's do it
a = 999
b = 909
pass 1:
rule 1: Add them but do not add carry and store in sum
as you can see we are adding value of a and b ad discarding all carry.
rule 2: Now add again but this time list out the carry’s and append 0 at end
as you can see we are adding value of a and b and storing only carry.
ex: 9+9 is 8 with carry 1 so we discard 8 and just take carry 1.
rule 3: stop if list carry become 0 else move sum to a and list carry to b and repeat all this.
carry is 1010 which is not equal to 0.
now a= 898
b=1010
and move to pass 2.
pass 2:
rule 1: Add them but do not add carry and store in sum
rule 2: Now add again but this time list out the carry’s and append 0 at end
rule 3: stop if list carry become 0 else move sum to a and list carry to b and repeat all this.
carry is 0100 which is not equal to 0.
now a= 1808
b=0100
and move to pass 3.
pass 3:
rule 1: Add them but do not add carry and store in sum
rule 2: Now add again but this time list out the carry’s and append 0 at end
rule 3: stop if list carry become 0 else move sum to a and list carry to b and repeat all this.
here carry is 0.hence we stop.
and sum is 1908
check answer with regular method
a = 999
+ b = 909
---------------
1908
---------------
we are correct.
let's write code:
But howto do this without using arithmetic operations ?
here we can use property of xor and And operator.
when we xor two no we get addition of two no without carry
and when we And two no we list out the carry’s.
As in rule 2 we have to append 0 at end.
To do this we can use left shift operator(<< ).
let a=2
b=3
in terms of binary
a=2=0010
b=3=0011
pass 1:
rule 1: Add them but do not add carry and store in sum
rule 2: Now add again but this time list out the carry’s and append 0 at end
rule 3: stop if list carry become 0 else move sum to a and list carry to b and repeat all this.
carry is 0100 which is not equal to 0.
now a= 0001
b=0100
and move to pass 2.
pass 2:
rule 1: Add them but do not add carry and store in sum
rule 2: Now add again but this time list out the carry’s and append 0 at end
rule 3: stop if list carry become 0 else move sum to a and list carry to b and repeat all this.
here carry is 0.hence we stop.
and sum is 101 which is binary of 5.
e.g.
as you can see during carry addition
i) If there is carry then it will go to next more significant digit
ii) If there is no carry than we put 0
Hence in both case rightmost digit is always 0.therefore we append 0 at end )
but today i am going to introduce new trick to add two numbers that you are never want to use in real life.
but it still worth and interviewers just love to see this method.
but it still worth and interviewers just love to see this method.
Add two numbers without using arithmetic operators
but before doing this let me introduce background for this method.
so lets start..
a = 999
b = 909
let me tell you rule how to add it.
rule 1: Add them but do not add carry and store in variable sum
rule 2: Now add again but this time list out the carry’s and append 0 at end and store in list carry
rule 3: stop if list carry become 0 else move sum to a and list carry to b and repeat all this.
let's do it
a = 999
b = 909
pass 1:
rule 1: Add them but do not add carry and store in sum
as you can see we are adding value of a and b ad discarding all carry.
rule 2: Now add again but this time list out the carry’s and append 0 at end
ex: 9+9 is 8 with carry 1 so we discard 8 and just take carry 1.
rule 3: stop if list carry become 0 else move sum to a and list carry to b and repeat all this.
carry is 1010 which is not equal to 0.
now a= 898
b=1010
and move to pass 2.
pass 2:
rule 1: Add them but do not add carry and store in sum
rule 2: Now add again but this time list out the carry’s and append 0 at end
rule 3: stop if list carry become 0 else move sum to a and list carry to b and repeat all this.
carry is 0100 which is not equal to 0.
now a= 1808
b=0100
and move to pass 3.
pass 3:
rule 1: Add them but do not add carry and store in sum
rule 2: Now add again but this time list out the carry’s and append 0 at end
here carry is 0.hence we stop.
and sum is 1908
check answer with regular method
a = 999
+ b = 909
---------------
1908
---------------
we are correct.
let's write code:
But howto do this without using arithmetic operations ?
here we can use property of xor and And operator.
when we xor two no we get addition of two no without carry
and when we And two no we list out the carry’s.
xor = Add them but do not add carry.
And = list out the carry’s
As in rule 2 we have to append 0 at end.
To do this we can use left shift operator(<< ).
Iterative code:
#include <stdio.h> int sum,listcarry; int Add(int a, int b) { while (1) { // rule 1: Add a and b but do not add carry and store in variable sum sum = a ^ b; //rule 2: Now add again but this time list out the carry’s //and append 0 at end and store in list carry listcarry = a & b; listcarry =listcarry << 1;//left shift operator(<<)to append 0 at end //rule 3: stop if list carry become 0 else move sum to a //and list carry to b and repeat all this. if(listcarry==0) break; else { a=sum; b=listcarry; } } return sum; // return sum; } int main() { printf("%d",Add(909, 999) ); return 0; }output:-
1908
Recursive code:
#include <stdio.h> int sum,listcarry=-1; int Add(int a, int b) { if(listcarry==0) return sum ; else { //rule 1: Add a and b but do not add carry and store in variable sum sum = a ^ b; //rule 2: Now add again but this time list out the carry’s //and append 0 at end and store in list carry listcarry = a & b; listcarry=listcarry << 1;//left shift operator(<<)to append 0 at end a=sum; b=listcarry; return Add(a, b) ; } } int main() { printf("%d",Add(909, 999) ); return 0; }
output:-
1908
click for code explanation
let a=2let a=2
b=3
in terms of binary
a=2=0010
b=3=0011
pass 1:
rule 1: Add them but do not add carry and store in sum
rule 2: Now add again but this time list out the carry’s and append 0 at end
rule 3: stop if list carry become 0 else move sum to a and list carry to b and repeat all this.
carry is 0100 which is not equal to 0.
now a= 0001
b=0100
and move to pass 2.
pass 2:
rule 1: Add them but do not add carry and store in sum
rule 2: Now add again but this time list out the carry’s and append 0 at end
rule 3: stop if list carry become 0 else move sum to a and list carry to b and repeat all this.
here carry is 0.hence we stop.
and sum is 101 which is binary of 5.
Why append 0 at end ?
( because carry is always added with next more significant digit.hence append 0 at end.e.g.
as you can see during carry addition
i) If there is carry then it will go to next more significant digit
ii) If there is no carry than we put 0
Hence in both case rightmost digit is always 0.therefore we append 0 at end )
0 comments: