Check if given number is a power of d where d is a power of 2 | C # program to find if a number is power of d where d is power of 2. ; Function to count the number of ways to paint N * 3 grid based on given conditions ; Check if there is only one bit set in n ; count 0 bits before set bit ; If count is a multiple of log2 ( d ) then return true else false ; If there are more than 1 bit set then n is not a power of 4 ; Driver Code
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 ; if ( n > 0 && ( n & ( n - 1 ) ) == 0 ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % ( Log2n ( d ) ) == 0 ) ; } return false ; } 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 ) ; } }