How to add two numbers with out using Arithmetic , union
operators in java....?

But we can use bitwise operators... but how...?

Answer Posted / midhula

public int addtwo(int a,int b)
{
if(b==0)
return a;

int sum = a ^ b; // ^ will be 1 if both operands are
//different otherwise 0.
int carry = (a & b) <<1; //& will be 1 if both operands are
//1 otherwise 0

return addtwo(sum,carry);
}

Is This Answer Correct ?    1 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is equals method in java?

522


How to calculate the length of a singly linked list in java?

581


What is the difference between a method and a function in alice?

574


What are thread groups?

568


What is formatted output?

509






Name the components that are termed to be Heavy-weight component but available in Light-weight components?

1982


What is meant by data hiding/encapsulation?

570


Can we declare a static variable inside a method?

526


State two differences between C and Java.

689


Can java list contain duplicates?

574


Can we override the overloaded method?

561


Explain the features of java?

589


What is function overriding and overloading in java?

577


Can we define constructor in inner class?

535


What is the default access specifier for variables and methods of a class?

566