Analyst Integration Question:

Download Job Interview Questions and Answers PDF

How do you find if a number is power of two, without using arithmetic operator?

Integration Programmer Interview Question
Integration Programmer Interview Question

Answer:

Assume its a question about using bitwise operator as soon as you hear restriction about not allowed to use arithmetic operator. If that restriction is not in place then you can easily check if a number is power of two by using modulus and division operator. By the using bitwise operator, there is a nice trick to do this. You can use following code to check if a number if power of two or not

public static boolean powerOfTwo(int x) {
return (x & (x - 1)) == 0;
}

x & (x-1) is a nice trick to convert right most bit to zero if it's on, I learned from hackers delight book.

Download Integration Programmer Interview Questions And Answers PDF

Previous QuestionNext Question
What is the difference between iteration and recursion?If I have a Web application that I find is still running (via top/ps/whatever) but users are getting "connection refused" when trying to access it, how would I go about diagnosing the problem?