Check if a Number is a Power of 2

View: 351    Dowload: 0   Comment: 0   Post by: hanhga   Category: Php&mySql   Fields: Other

9 point/3 review File has been tested

How to check if a number is a power of 2.  To understand this question, let’s take some example.

Example –

Input – 16   – 16 is a power of 2 (2^4).

Input – 15 – 15 is not a power of 2.

Input – 32- 32 is a power of 2 (2^5).

We can use multiple approaches to check whether a number is a power of 2 or not.

How to Check if a Number is a Power of 2

Method 1 : Check whether a number is perfectly divisible by 2.

In this approach, We’ll check whether a number is perfectly divisible by 2. If a number is perfectly divisible by 2 then it’s remainder is 0.

We’ll repeat this step until n becomes 1. In each step, we check if n%2 becomes non-zero and n is not 1 then n is not a power of 2. If n becomes 1 then it is a power of 2.

Method 2 : Bit Manipulation

Let’s consider the binary representation of any number that is a power of 2. We’ll find only one bit is set in their binary representation.

2    =  00000010 
4    =  00000100 
8    =  00001000 
16   =  00010000 

And number which is one less has following binary representation

1 = 00000001 
3 = 00000011 
7 = 00000111
   0 0 0 0 0 0 1 0  (Binary representation of 2)
&  0 0 0 0 0 0 0 1  (Binary representation of 1)
-------------------
   0 0 0 0 0 0 0 0

We have discussed two approaches to check whether a number is a power of 2 or not. Let’s implement both the approaches in multiple programming languages.

C Code to Check whether a Number is a Power of 2

METHOD 1: Check if a number is perfectly divisible by 2

#include <stdio.h>
int main(void) {
 
 int num, flag = 1;
        printf("Enter a number \n");
        scanf("%d", &num);
 
 while(num > 1) {
 
 if(num % 2 != 0){
      flag = 0;
      break;
 }
 
 num = num / 2;
 }
 
 if(flag) {
 printf("Input number is a power of 2");
 } else {
 printf("Input number is not a power of 2");
 }
 
 return 0;
}

METHOD 2 – Bit Manipulation

#include <stdio.h>
 
int isPowerOfTwo(int num) {
 return num && !(num & (num-1)); 
}
int main(void) { 
 int num;
 
 printf("Enter a number\n");
 scanf("%d", &num);
 
 if(isPowerOfTwo(num)) {
     printf("Input number is a power of 2");
 } else {
     printf("Input number is not a power of 2");
 }
 
 return 0;
}

Java Code to Check if a Number is a Power of 2

METHOD 1: Check if a number is perfectly divisible by 2

import java.util.*;
 
public class PowerofTwo {
 
    public static void main(String[] args) {
        
        int num, flag = 1;
        
        System.out.println("Enter a number");
        Scanner in = new Scanner(System.in);
       
        num = in.nextInt();
 
        while(num > 1) {
            
            if( num % 2 != 0) {
                flag = 0;
                break;
            }
            
            num = num/2;
        }
        
        if(flag == 1)
            System.out.println("Input number is a power of 2");
        else
            System.out.println("Input number is not a power of 2");
    }
    
}

METHOD 2 – Bit Manipulation

import java.util.*;
 
public class PowerofTwo {
 
    
    public static boolean isPowerOfTwo(int num) {
        
        return (num > 0) && ((num & (num - 1)) == 0);
    }
    
    public static void main(String[] args) {
        
        int num;
        
        System.out.println("Enter a number");
        Scanner in = new Scanner(System.in);
        
        num = in.nextInt();
        
        if(isPowerOfTwo(num))
            System.out.println("Input number is a power of 2");
        else
            System.out.println("Input number is not a power of 2");
    }
    
}

Check if a Number is a Power of 2

Check if a Number is a Power of 2 Posted on 12-10-2016  How to check if a number is a power of 2. To understand this question, let’s take some example. 3/10 351

Comment:

To comment you must be logged in members.

Files with category

  • PHP source code: get content of google search

    PHP source code: get content of google search

    View: 26    Download: 1   Comment: 0

    Category: Php&mySql     Fields: Other

    How to get the results of google search and display on your website. This PHP code can do that. Refer to the following code.

  • PHP as a Scripting Language for C#

    PHP as a Scripting Language for C#

    View: 35    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    When creating .NET applications (including desktop and web applications), it may be useful to allow extending the application using some scripting language. The users of the application can write simple scripts to configure the application, modify...

  • How to Picking the Brains of Your Customers with Microsoft’s Text Analytics

    How to Picking the Brains of Your Customers with Microsoft’s Text Analytics

    View: 4113    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    With the explosion of machine learning services in recent years, it has become easier than ever for developers to create “smart apps”. In this article, I’ll introduce you to Microsoft’s offering for providing machine-learning capabilities to apps.

  • How to MySqli Tutorial PHP MySqli Extension

    How to MySqli Tutorial PHP MySqli Extension

    View: 414    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    PHP provides three api to connect mysql Database.

  • Make Laravel Artisan Commands

    Make Laravel Artisan Commands

    View: 394    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    Artisan is the command line tool used in Laravel framework. It offers a bunch of useful command that can help you develop application quickly. Apart from Artisan available commands, you can create your own custom commands to improve your workflow.

  • Check if a Number is a Power of 2

    Check if a Number is a Power of 2

    View: 351    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    How to check if a number is a power of 2. To understand this question, let’s take some example.

  • Concatenate columns in MySql

    Concatenate columns in MySql

    View: 418    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    Artisan is the command line tool used in Laravel framework. It offers a bunch of useful command that can help you develop application quickly. Apart from Artisan available commands, you can create your own custom commands to improve your workflow

  • How to Query NULL Value in MySql

    How to Query NULL Value in MySql

    View: 346    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    Misunderstanding NULL is common mistake beginners do while writing MySql query. While quering in MySql they compare column name with NULL. In MySql NULL is nothing or in simple word it isUnknown Value so if you use comparison operator for NULL values...

 
File suggestion for you
File top downloads
Codetitle.net - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.net Develope by Vinagon .Ltd