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

You can create a string object as string str = “abc”; why cant a button object be created as button bt = “abc”;? Explain

725


Does collectionutils isempty check for null?

1103


Write a program to find the greatest of three numbers in java?

755


What is assembly language?

719


How do you define a set in java?

718


Can a constructor have different name than a class name in java?

809


What is the escape character in java?

711


What is identifier with example?

787


What is meant by 'Class access modifiers'?

788


What is the maximum size of array in java?

719


What are the benefits of immutable objects?

744


How will you calculate the depth of a binary tree if the tree contains 15 nodes?

818


What is module in project?

718


Can we have try without catch block?

845


What are locale settings?

727