Rabu, 23 Juli 2014

How to Check if Integer Number is Power of Two in Java - 3 examples




How to check if an integer number is power of 2 in Java is one of the popular
programming
interview question
and has been asked in many interviews. Surprisingly,
this problem which looks simple enough to answer, doesn't turn out that simple
if for many developers. Many Java programmers, both freshers and less
experienced,  struggle to write code for a
function, which can check if number is power of 2 or not. There could be many
different reasons for that, but it’s expected to at least come up with brute
force solution. For those who are familiar with bitwise
operators in Java
, how positive and negative numbers are represented in
binary format, this exercise is quite easy. Since negative numbers are
represented as 2's complement value in Java, you can easily find if any number
is power of 2 or not by looking at its bit pattern. Remember checking for power
of two is different than checking
if number is even or odd
, that’s another thing to note. A number can be
even, but it’s not necessary to be a power of 
two, e.g. 6 is even but it’s not a power of two.

 


3 ways to check if number is power of two or
not



Power of Two number checking in JavaIn this article we will see 3 simple examples to check if any integer
number is power of 2 or not. We have three methods, which uses bitwise operator,
brute force way and bit shit operators to solve this problem. Method which uses
bitwise operators can not check if zero is power of 2 or not, and only work for
integer number which is greater than or equal to 1. here are code example of 3
simple method to find out if a number is power of 2 or not :










public class PowerOf2Test
{





    public static void main(String args[])
{





        int[] numbers = {0,1,2,6,8};


      


        for(int num: numbers){


            System.out.println("isPowerOfTwo()--
is "
+ num + " power of two in Java :" + isPowerOfTwo(num));


            System.out.println("powerOfTwo()--
is "
+ num + " power of two in Java :" +
powerOfTwo(num));


            System.out.println("checkPowerOfTwo()--
is "
+ num + " power of two in Java :" +
checkPowerOfTwo(num));


            System.out.println("-----------------------------------------------------------");


        }         





    }


    /*


     * checking if number is power of 2 using
bit shift operator in java


     * e.g. 4 in binary format is "0000
0000 0000 0000 0000 0000 0000 0100";


     * and -4 is                  "1111 1111 1111 1111
1111 1111 1111 1100";


     * and 4&-4 will be           "0000 0000 0000 0000 0000 0000
0000 0100"


     */


    private static boolean isPowerOfTwo(int number)
{


        if(number <=0){


            throw new
IllegalArgumentException(
"number: " + number);


        }


        if ((number & -number) == number) {


            return true;


        }


        return false;


    }


  


    /*


     * checking if number is power of 2 using
brute force


     * starts with 1, multiplying with 2 it
will eventually be same as original number


     */


    private static boolean powerOfTwo(int number){


        int square = 1;


        while(number >= square){


            if(number == square){


                return true;


            }


            square = square*2;


        }


        return false;


    }


  


    /*


     * find if an integer number is power of 2
or not using bit shift operator


     */


  


    private static boolean checkPowerOfTwo(int number){


         if(number <=0){


            throw new
IllegalArgumentException(
"number: " + number);


        }


        return ((number & (number -1)) == 0);


    }


}





Output:


isPowerOfTwo()-- is 0 power of two in
Java
:true


powerOfTwo()-- is 0 power of two in
Java
:false


checkPowerOfTwo()-- is 0 power of two in
Java
:true


-----------------------------------------------------------


isPowerOfTwo()-- is 1 power of two in
Java
:true


powerOfTwo()-- is 1 power of two in
Java
:true


checkPowerOfTwo()-- is 1 power of two in
Java
:true


-----------------------------------------------------------


isPowerOfTwo()-- is 2 power of two in
Java
:true


powerOfTwo()-- is 2 power of two in
Java
:true


checkPowerOfTwo()-- is 2 power of two in
Java
:true


-----------------------------------------------------------


isPowerOfTwo()-- is 6 power of two in
Java
:false


powerOfTwo()-- is 6 power of two in
Java
:false


checkPowerOfTwo()-- is 6 power of two in
Java
:false


-----------------------------------------------------------


isPowerOfTwo()-- is 8 power of two in
Java
:true


powerOfTwo()-- is 8 power of two in
Java
:true


checkPowerOfTwo()-- is 8 power of two in
Java
:true


-----------------------------------------------------------








That’s all on this article about checking if a number I power of two or
not.  Let us know if you find another way
to verify if number is power of two.








Related Java Coding Questions from Javarevisited Blog





























Source:http://javarevisited.blogspot.com/2013/05/how-to-check-if-integer-number-is-power-of-two-example.html

1 komentar:

  1. Awesome!! You got the best article on this topic. You're doing a great job.

    Thanks for sharing.
    http://www.flowerbrackets.com/java-program-to-find-even-or-odd/

    BalasHapus