How to find whether a number is a power of multiple of 2 (bitwise)

Hi Everyone, This blog post will help you in teaching how to find whether a number is a power of multiple of 2.  This program helps in checking, whether a number is a power of 4, 16, 32 etc. For checking whether a number is a power of 2, refer my previous post - How to check whether a number is a power of 2.

When we are asked a question to check whether a number is a power of 16 or 32, the usual method which comes in our mind is to check using logarithms. But this method is not preferred when relating to  performance issues. So, we go by the bitwise method. It is as follows.

The program is as follows:

1) First we check whether the number is a power of 2. If it is not, then we are returning false.

2) Then we have to right shift the number and " the other number which is a multiple of 2" until it is greater than 1 and count the number of shifts of each.

3) If the count of shifts of the number to be checked is a multiple of count of shifts of the number which is a multiple of 2, then the number to be checked is a power of " the other number which is a power of 2".

The source code of the program is as follows. I have coded the program in the manner in which it is coded in industries.

 public class PowerOfMultipleOf2 {  
      public static void main(String[] args)   
      {  
           int num1 = 4096, num2 = 8192, num3 = 65536;  
           int check1 = 16, check2 = 8;  
           boolean bool1 = checkMultiple(num1,check1);  
           if(bool1)  
           {  
           System.out.println(num1 + " is a power of "+check1);  
           }  
           else  
           {  
           System.out.println(num1 + " is not a power of "+check1);  
           }  
           System.out.println();  
           boolean bool2 = checkMultiple(num2,check1);  
           if(bool2)  
           {  
           System.out.println(num2 + " is a power of "+check1);  
           }  
           else  
           {  
           System.out.println(num2 + " is not a power of "+check1);  
           }  
           System.out.println();  
           boolean bool3 = checkMultiple(num3,check2);  
           if(bool3)  
           {  
           System.out.println(num3 + " is a power of "+check2);  
           }  
           else  
           {  
           System.out.println(num3 + " is not a power of "+check2);  
           }  
      }  
      public static boolean checkMultiple(int num, int check)  
      {  
           if((num & (num-1)) != 0)  
           return false;  
           int numOfZeros = 0; // variable for counting the number of zeros of the number  
           int checkZeros = 0; // variable for counting the number of zeros of the power of the number  
           int checknum = check;  
           while(checknum > 1)  
           {  
                checknum = checknum >> 1;  
                numOfZeros++;  
           }  
           while(num>1)  
           {  
                num=num >> 1;  
                checkZeros++;  
           }  
           if((checkZeros % numOfZeros)==0)  
           {  
                return true;  
           }  
           else  
           {  
                return false;  
           }  
      }  
 }  

The output of the above program is as follows: 

 4096 is a power of 16  
 8192 is not a power of 16  
 65536 is not a power of 8  

I hope everyone liked the blog post. Please comment below if you have any doubt. All comments will be responded soon.

Thank you for reading the post.

Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++