Given an integer n, find whether it is a power of d or not, where d is itself a power of 2.

**Examples:**

Input : n = 256, d = 16 Output : Yes Input : n = 32, d = 16 Output : No

**Method 1** Take log of the given number on base d, and if we get an integer then number is power of d.

**Method 2** Keep dividing the number by d, i.e, do n = n/d iteratively. In any iteration, if n%d becomes non-zero and n is not 1 then n is not a power of d, otherwise n is a power of d.

**Method 3(Bitwise)**

A number n is a power of d if following conditions are met.

a) There is only one bit set in the binary representation of n (Note : d is a power of 2)

b) The count of zero bits before the (only) set bit is a multiple of log_{2}(d).

For example: For n = 16 (10000) and d = 4, 16 is a power of 4 because there is only one bit set and count of 0s before the set bit is 4 which is a multiple of log_{2}(4).

## C++

`// CPP program to find if a number is power ` `// of d where d is power of 2. ` `#include<stdio.h> ` ` ` `unsigned ` `int` `Log2n(unsigned ` `int` `n) ` `{ ` `return` `(n > 1)? 1 + Log2n(n/2): 0; ` `} ` ` ` `bool` `isPowerOfd(unsigned ` `int` `n, unsigned ` `int` `d) ` `{ ` `int` `count = 0; ` ` ` `/* Check if there is only one bit set in n*/` `if` `(n && !(n&(n-1)) ) ` `{ ` ` ` `/* count 0 bits before set bit */` ` ` `while` `(n > 1) ` ` ` `{ ` ` ` `n >>= 1; ` ` ` `count += 1; ` ` ` `} ` ` ` ` ` `/* If count is a multiple of log2(d) ` ` ` `then return true else false*/` ` ` `return` `(count%(Log2n(d)) == 0); ` `} ` ` ` `/* If there are more than 1 bit set ` ` ` `then n is not a power of 4*/` `return` `false` `; ` `} ` ` ` `/* Driver program to test above function*/` `int` `main() ` `{ ` `int` `n = 64, d = 8; ` `if` `(isPowerOfd(n, d)) ` ` ` `printf` `(` `"%d is a power of %d"` `, n, d); ` `else` ` ` `printf` `(` `"%d is not a power of %d"` `, n, d); ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find if ` `// a number is power of d ` `// where d is power of 2. ` ` ` `class` `GFG ` `{ ` `static` `int` `Log2n(` `int` `n) ` `{ ` ` ` `return` `(n > ` `1` `)? ` `1` `+ ` ` ` `Log2n(n / ` `2` `): ` `0` `; ` `} ` ` ` `static` `boolean` `isPowerOfd(` `int` `n, ` ` ` `int` `d) ` `{ ` `int` `count = ` `0` `; ` ` ` `/* Check if there is ` `only one bit set in n*/` `if` `(n > ` `0` `&& (n & ` `(n - ` `1` `)) == ` `0` `) ` `{ ` ` ` `/* count 0 bits ` ` ` `before set bit */` ` ` `while` `(n > ` `1` `) ` ` ` `{ ` ` ` `n >>= ` `1` `; ` ` ` `count += ` `1` `; ` ` ` `} ` ` ` ` ` `/* If count is a multiple ` ` ` `of log2(d) then return ` ` ` `true else false*/` ` ` `return` `(count % ` ` ` `(Log2n(d)) == ` `0` `); ` `} ` ` ` `/* If there are more ` `than 1 bit set then ` `n is not a power of 4*/` `return` `false` `; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `64` `, d = ` `8` `; ` ` ` `if` `(isPowerOfd(n, d)) ` ` ` `System.out.println(n + ` ` ` `" is a power of "` `+ d); ` ` ` `else` ` ` `System.out.println(n + ` ` ` `" is not a power of "` `+ d); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find if a number ` `# is power of d where d is power of 2. ` ` ` `def` `Log2n(n): ` ` ` `return` `(` `1` `+` `Log2n(n ` `/` `2` `)) ` `if` `(n > ` `1` `) ` `else` `0` `; ` ` ` `def` `isPowerOfd(n, d): ` ` ` `count ` `=` `0` `; ` ` ` ` ` `# Check if there is only ` ` ` `# one bit set in n ` ` ` ` ` `if` `(n ` `and` `(n & (n ` `-` `1` `))` `=` `=` `0` `): ` ` ` `# count 0 bits ` ` ` `# before set bit ` ` ` `while` `(n > ` `1` `): ` ` ` `n >>` `=` `1` `; ` ` ` `count ` `+` `=` `1` `; ` ` ` `# If count is a multiple of log2(d) ` ` ` `# then return true else false ` ` ` `return` `(count` `%` `(Log2n(d)) ` `=` `=` `0` `); ` ` ` ` ` `# If there are more than 1 bit set ` ` ` `# then n is not a power of 4 ` ` ` `return` `False` `; ` ` ` `# Driver Code ` `n ` `=` `64` `; ` `d ` `=` `8` `; ` `if` `(isPowerOfd(n, d)): ` ` ` `print` `(n,` `"is a power of"` `,d); ` `else` `: ` ` ` `print` `(n,` `"is not a power of"` `,d); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find if ` `// a number is power of d ` `// where d is power of 2. ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `Log2n(` `int` `n) ` `{ ` ` ` `return` `(n > 1)? 1 + ` ` ` `Log2n(n / 2): 0; ` `} ` ` ` `static` `bool` `isPowerOfd(` `int` `n, ` ` ` `int` `d) ` `{ ` `int` `count = 0; ` ` ` `/* Check if there is ` `only one bit set in n*/` `if` `(n > 0 && (n & (n - 1)) == 0) ` `{ ` ` ` `/* count 0 bits ` ` ` `before set bit */` ` ` `while` `(n > 1) ` ` ` `{ ` ` ` `n >>= 1; ` ` ` `count += 1; ` ` ` `} ` ` ` ` ` `/* If count is a multiple ` ` ` `of log2(d) then return ` ` ` `true else false*/` ` ` `return` `(count % (Log2n(d)) == 0); ` `} ` ` ` `/* If there are more than ` `1 bit set then n is not ` `a power of 4*/` `return` `false` `; ` `} ` ` ` `// Driver Code ` `static` `void` `Main() ` `{ ` `int` `n = 64, d = 8; ` `if` `(isPowerOfd(n, d)) ` ` ` `Console.WriteLine(` `"{0} is a "` `+ ` ` ` `"power of {1}"` `, ` ` ` `n, d); ` `else` ` ` `Console.WriteLine(` `"{0} is not a"` `+ ` ` ` `" power of {1}"` `, ` ` ` `n, d); ` `} ` ` ` `// This code is contributed by mits ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find if a number ` `// is power of d where d is power of 2. ` ` ` `function` `Log2n(` `$n` `) ` `{ ` ` ` `return` `(` `$n` `> 1)? 1 + ` ` ` `Log2n(` `$n` `/ 2): 0; ` `} ` ` ` `function` `isPowerOfd(` `$n` `, ` `$d` `) ` `{ ` ` ` `$count` `= 0; ` ` ` `// Check if there is only ` `// one bit set in n ` `if` `(` `$n` `&& !(` `$n` `& (` `$n` `- 1))) ` `{ ` ` ` ` ` `// count 0 bits ` ` ` `// before set bit ` ` ` `while` `(` `$n` `> 1) ` ` ` `{ ` ` ` `$n` `>>= 1; ` ` ` `$count` `+= 1; ` ` ` `} ` ` ` ` ` `/* If count is a multiple of log2(d) ` ` ` `then return true else false*/` ` ` `return` `(` `$count` `%(Log2n(` `$d` `)) == 0); ` `} ` ` ` ` ` `/* If there are more than 1 bit set ` ` ` `then n is not a power of 4*/` ` ` `return` `false; ` `} ` ` ` `// Driver Code ` `$n` `= 64; ` `$d` `= 8; ` `if` `(isPowerOfd(` `$n` `, ` `$d` `)) ` ` ` `echo` `$n` `,` `" "` `,` `"is a power of "` `, ` `$d` `; ` `else` ` ` `echo` `$n` `,` `" "` `,` `"is not a power of "` `, ` `$d` `; ` ` ` `// This code is contributed by m_kit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

64 is a power of 8

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Given a HUGE number check if it's a power of two.
- Check whether a given Number is Power-Isolated or not
- Check if a number is a power of another number
- Check if the number is a Prime power number
- Check if a large number is divisible by a number which is a power of 2
- Find power of power under mod of a prime
- Compute power of power k times % m
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Find the sum of power of bit count raised to the power B
- Sum of digits of a given number to a given power
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Check if a number can be expressed as x^y (x raised to power y)
- Check if a number is power of k using base changing method
- Check if a number can be expressed as power | Set 2 (Using Log)
- Check if ceil of number divided by power of two exist in sorted array
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Find whether a given number is a power of 4 or not
- Highest power of 2 less than or equal to given number
- Find the super power of a given Number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.