Check if an Integer is a Power of 8

Last Updated : 20 Jul, 2026

Given a positive integer n, check whether it can be represented as a power of 8.

Examples : 

Input: n = 64
Output: true
Explanation: 64 = 82, so it is a power of 8. Therefore, the output is true.

Input: n = 75
Output: false
Explanation: 75 cannot be expressed as 8x for any non-negative integer x. Therefore, the output is false.

Try It Yourself
redirect icon

[Naive Approach] Generate Powers of 8 - O(log n) Time and O(1) Space

Start from 1 and keep multiplying by 8 until the value becomes equal to or greater than n. If it becomes equal to n, return true.

C++
#include <iostream>
using namespace std;

bool isPowerOfEight(int n) {
    int val = 1;

    while (val < n) {
        val *= 8;
    }

    return val == n;
}

int main() {
    cout << (isPowerOfEight(64) ? "true" : "false") << endl;
    cout << (isPowerOfEight(75) ? "true" : "false") << endl;

    return 0;
}
Java
class GFG {
    static boolean isPowerOfEight(int n) {
        int val = 1;

        while (val < n) {
            val *= 8;
        }

        return val == n;
    }

    public static void main(String[] args) {
        System.out.println(isPowerOfEight(64));
        System.out.println(isPowerOfEight(75));
    }
}
Python
def isPowerOfEight(n):
    val = 1

    while val < n:
        val *= 8

    return val == n


if __name__ == "__main__":
    print("true" if isPowerOfEight(64) else "false")
    print("true" if isPowerOfEight(75) else "false")
C#
using System;

class GFG {
    static bool isPowerOfEight(int n) {
        int val = 1;

        while (val < n) {
            val *= 8;
        }

        return val == n;
    }

    static void Main() {
        Console.WriteLine(isPowerOfEight(64) ? "true" : "false");
        Console.WriteLine(isPowerOfEight(75) ? "true" : "false");
    }
}
JavaScript
function isPowerOfEight(n) {
    let val = 1;

    while (val < n) {
        val *= 8;
    }

    return val === n;
}

// Driver Code
console.log(isPowerOfEight(64));
console.log(isPowerOfEight(75));

Output
true
false

[Expected Approach] Bit Manipulation - O(1) Time & O(1) Space

Every power of 8 is also a power of 2, and its only set bit appears at a position that is a multiple of 3.

C++
#include <iostream>
using namespace std;

bool isPowerOfEight(int n) {
    return n > 0 && (n & (n - 1)) == 0 && (n & 0x49249249);
}

int main() {
    cout << (isPowerOfEight(64) ? "true" : "false") << endl;
    cout << (isPowerOfEight(75) ? "true" : "false") << endl;

    return 0;
}
Java
class GFG {
    static boolean isPowerOfEight(int n) {
        return n > 0 && (n & (n - 1)) == 0
            && (n & 0x49249249) != 0;
    }

    public static void main(String[] args) {
        System.out.println(isPowerOfEight(64));
        System.out.println(isPowerOfEight(75));
    }
}
Python
def isPowerOfEight(n):
    return n > 0 and (n & (n - 1)) == 0 and (n & 0x49249249) != 0


if __name__ == "__main__":
    print("true" if isPowerOfEight(64) else "false")
    print("true" if isPowerOfEight(75) else "false")
C#
using System;

class GFG {
    static bool isPowerOfEight(int n) {
        return n > 0 && (n & (n - 1)) == 0
            && (n & 0x49249249) != 0;
    }

    static void Main() {
        Console.WriteLine(isPowerOfEight(64) ? "true" : "false");
        Console.WriteLine(isPowerOfEight(75) ? "true" : "false");
    }
}
JavaScript
function isPowerOfEight(n) {
    return n > 0 && (n & (n - 1)) === 0
           && (n & 0x49249249) !== 0;
}

// Driver Code
console.log(isPowerOfEight(64));
console.log(isPowerOfEight(75));

Output
true
false
Comment