How to check if number is power of two

In this tutorial, we will see how to check if number is power of two.
There are many approaches to check if number is power of two or not.

Approach 1:

It is very easy and straight forward approach.

• Run a while loop which checks for condition if n is even number (n%2==0).
• If n is even then divide it by 2 in each iteration.
• When you get out of while loop and n is equal to 1 then number is power of two,
• If number is not equal to 1 then number is not power of two.

Approach 2:

We can use bitwise and operator to check if number is power of two or not.

It will a very simple way to check if number is power of two. Let’s see how it works.
Let’s say n is 8. Its binary representation will be : 1000.
binary represetation of 7 will be : 0111.
1 0 0 0
& 0 1 1 1
———-
0 0 0 0
———-
If number is power of 2, then it will have only one bit set to “1”.
For example:
8 :   1000
32 : 100000

Similarly when you  check binary form of 7 and 31, it will have all the bits set to “1”
7 :   111
31:  11111
so if you apply bitwise & operator on n and n-1 and result is 0. It means number is power of two.

Java program to check if number is power of two:

When you run above code, you will get below results

import_contacts

Related Posts

• 05 April

Count occurrences of Character in String in Java

Table of Contents1. Using String Library Methods2. Using Recursion3. Using Hashing ConceptUsing ArraysUsing Collections (Map) 4. Using Java 8 FeaturesWas this post helpful? In this article, we will look at a problem: Given an Input String and a Character, we have to Count Occurrences Of character in String in java. For Example, If the Given […]

• 24 December

How to capitalize first letter in java

Learn about how to capitalize first letter in java.

• 09 October

Java program to find first and last digit of a number

Table of ContentsAlgorithmUsing while loopUsing log() and pow() methodsUsing while loop and pow() methodWas this post helpful? In this article, we are going to find first and last digit of a number in Java. To find first and last digit of any number, we can have several ways like using modulo operator or pow() and […]

• 03 October

Happy Number program in Java

Table of ContentsWhat is a Happy Number?Using HashsetAlgorithmExampleUsing slow and fast pointersAlgorithmExampleWas this post helpful? In this article, we are going to learn to find Happy Number using Java. Let’s first understand, what is Happy Number? What is a Happy Number? A number which leaves 1 as a result after a sequence of steps and […]

• 30 September

Find Perfect Number in Java

Table of ContentsIterative approachRecursive approachWas this post helpful? In this article, we are going to find whether a number is perfect or not using Java. A number is called a perfect number if the sum of its divisors is equal to the number. The sum of divisors excludes the number. There may be several approaches […]

• 28 September

How to find Magic Number in Java

Table of ContentsWhat is a Magic Number?Algorithm for Magic NumberExample to find Magic NumberAnother Example To find Magic NumberWas this post helpful? In this article, we are going to learn to find Magic Number using Java. Let’s first understand, what is Magic Number? What is a Magic Number? A number which leaves 1 as a […]

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.