Write a program to find out Number Is Power Of 2 Or Not In Java


In this tutorial we will how to find out Number Is Power Of 2 Or Not in java.

Contents of page >
  • Program/Example 1 : To find out Number Is Power Of 2 Or Not In Java
  • Program/Example 2 : To find out Number Is Power Of 2 Or Not In Java


Program/Example 1 : To find out Number Is Power Of 2 Or Not In Java
We will use (number & (number - 1)) == 0 to find out Number Is Power Of 2 Or Not In Java.

If (number & (number - 1)) == 0 returns true then number is power of two.
Else number is power not of two.
import java.util.Scanner;
/*
* Write a program to Find Number Is Power Of 2 Or Not In Java
*/
public class CheckNumberIsPowerOf2OrNotExample {
   public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          System.out.print("Enter number : ");
          int number = scanner.nextInt();
          if ((number & (number - 1)) == 0)
                 System.out.println(number + " is power of two");
          else
                 System.out.println(number + " is not power of two");
   }
}
/*OUTPUT
Enter number : 8
8 is power of two
*/

If you pass 5 then output will be >
Enter number : 5
5 is not power of two

Let’s understand logic behind ((number & (number - 1)) == 0), where number = 8 to find number is power of two or not in java.

Step 1 > find binary form of int 8
In binary form int 8 will look like this - because int in 32 bit datatype
8 (in binary form) = 0000 0000 0000 0000 0000 0000 0000 1000

Step 2 > find binary form of int 1
Now, when we subtract 1 from above.
1 (in binary form) =  0000 0000 0000 0000 0000 0000 0000 0001

Step 3 > subtract binary 1 from binary 8
 0000 0000 0000 0000 0000 0000 0000 1000       
-
 0000 0000 0000 0000 0000 0000 0000 0001      
------------------------------------------------------------
= 0000 0000 0000 0000 0000 0000 0000 0101

Step 4 > Do bitwise & (bitwise AND) operation i.e.  ((number & (number - 1)) == 0)

First let’s see bitwise & operation table  Read Bitwise operators in java




Bitwise Operators
&
bitwise AND operator

operator1
operator2
bitwise AND operator
Result
0
0
0
0
1
0
1
0
0
1
1
1


Now, finally let’s evaluate ((number & (number - 1)) == 0)

  0000 0000 0000 0000 0000 0000 0000 1000   ((number = 8) from step 1)
  &
  0000 0000 0000 0000 0000 0000 0000 0101   ((number -1) from step 3)
= 0000 0000 0000 0000 0000 0000 0000 0000  (i.e. = 0) (Refer above bitwise AND operator table)

So, ((8 & (8 - 1)) == 0) that means  number is power of two




Program/Example 2 : To find out Number Is Power Of 2 Or Not In Java

We will use (number & -number ) == number to find out Number Is Power Of 2 Or Not In Java.

If (number & -number ) == number returns true then number is power of two.
Else number is power not of two.
import java.util.Scanner;
/*
* Write a program to Find Number Is Power Of 2 Or Not In Java
*/
public class CheckNumberIsPowerOf2OrNotExample2 {
   public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          System.out.print("Enter number : ");
          int number = scanner.nextInt();
          if ((number & -number) == number)
                 System.out.println(number + " is power of two");
          else
                 System.out.println(number + " is not power of two");
   }
}
/*OUTPUT
Enter number : 16
16 is power of two
*/

If you pass 5 then output will be >
Enter number : 5
5 is not power of two

Let’s understand logic behind ((number & -number ) == number), where number = 8 to find number is power of two or not in java

Step 1 > find binary form of int 8
In binary form int 8 will look like this - because int in 32 bit datatype
8 (in binary form) = 0000 0000 0000 0000 0000 0000 0000 1000

Step 2 > find binary form of int -8
-8 (in binary form) =  1111 1111 1111 1111 1111 1111 1111 1000

Step 3 > Do bitwise & (bitwise AND) operation i.e.  ((number & -number ) == number)

First let’s see bitwise & operation table  Read Bitwise operators in java

  0000 0000 0000 0000 0000 0000 0000 1000 (number = 8 from step 1)
&
  1111  1111  1111  1111 1111  1111  1111 1000 (number = -8 from step 1)
------------------------------------------------------------
= 0000 0000 0000 0000 0000 0000 0000 1000      (i.e. = 8)

So,  ((8 & 8) == 8) that means  number is power of two.



Summary -
So in this tutorial we will how to find out Number Is Power Of 2 Or Not in java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

write grade program using switch statement in java

Write a program to find GCD (or HCF) of Two Numbers In Java

program to find LCM of Two Numbers In Java


No comments:

Post a Comment