How to write 4 different functions that check if a number is a power of 2 in C

3 Answers

0 votes
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdbool.h>
 
// 1) Bitwise trick: x & (x - 1) == 0
bool is_power_of_two_a(unsigned int x) {
    return x != 0 && (x & (x - 1)) == 0;
}
 
// 2) Count bits using builtin popcount (GCC/Clang)
bool is_power_of_two_b(unsigned int x) {
    return x != 0 && __builtin_popcount(x) == 1;
}
 
// 3) Manual bit counting (portable)
bool is_power_of_two_c(unsigned int x) {
    int count = 0;
    while (x) {
        count += x & 1;
        x >>= 1;
    }
    return count == 1;
}
 
// 4) Convert to binary string and count '1'
bool is_power_of_two_d(unsigned int x) {
    if (x == 0) return false;
 
    char bin[64] = "";
    int idx = 0;
 
    while (x) {
        bin[idx++] = (x & 1) ? '1' : '0';
        x >>= 1;
    }
    bin[idx] = '\0';
 
    int ones = 0;
    for (int i = 0; i < idx; i++)
        if (bin[i] == '1') ones++;
 
    return ones == 1;
}
 
int main() {
    unsigned int a = 64;   // true
    unsigned int b = 70;   // false
 
    printf("Testing %u:\n", a);
    printf("%s\n", is_power_of_two_a(a) ? "true" : "false");
    printf("%s\n", is_power_of_two_b(a) ? "true" : "false");
    printf("%s\n", is_power_of_two_c(a) ? "true" : "false");
    printf("%s\n", is_power_of_two_d(a) ? "true" : "false");
 
    printf("\nTesting %u:\n", b);
    printf("%s\n", is_power_of_two_a(b) ? "true" : "false");
    printf("%s\n", is_power_of_two_b(b) ? "true" : "false");
    printf("%s\n", is_power_of_two_c(b) ? "true" : "false");
    printf("%s\n", is_power_of_two_d(b) ? "true" : "false");
 
    return 0;
}
 
 
/*
OUTPUT:
 
Testing 64:
true
true
true
true
 
Testing 70:
false
false
false
false
 
*/

 



answered Apr 2 by avibootz
edited Apr 2 by avibootz
0 votes
#include <stdio.h>
#include <stdbool.h>

/* 1. Bit‑hack: n is power of 2 if n > 0 and n & (n - 1) == 0 */
bool is_power_of_two_bitwise(unsigned int n) {
    return n > 0 && (n & (n - 1)) == 0;
}

/* 2. Loop dividing by 2 */
bool is_power_of_two_loop(unsigned int n) {
    if (n == 0) return false;
    while (n % 2 == 0) {
        n /= 2;
    }
    return n == 1;
}

/* 3. Count number of set bits */
bool is_power_of_two_popcount(unsigned int n) {
    if (n == 0) return false;
    int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
        if (count > 1) return false;
    }
    return count == 1;
}

/* 4. Using recursion */
bool is_power_of_two_recursive(unsigned int n) {
    if (n == 1) return true;
    if (n == 0 || n % 2 != 0) return false;
    return is_power_of_two_recursive(n / 2);
}

int main(void) {
    unsigned int a = 16;
    unsigned int b = 18;

    printf("Testing %u:\n", a);
    printf("bitwise:   %s\n", is_power_of_two_bitwise(a) ? "true" : "false");
    printf("loop:      %s\n", is_power_of_two_loop(a) ? "true" : "false");
    printf("popcount:  %s\n", is_power_of_two_popcount(a) ? "true" : "false");
    printf("recursive: %s\n", is_power_of_two_recursive(a) ? "true" : "false");

    printf("\nTesting %u:\n", b);
    printf("bitwise:   %s\n", is_power_of_two_bitwise(b) ? "true" : "false");
    printf("loop:      %s\n", is_power_of_two_loop(b) ? "true" : "false");
    printf("popcount:  %s\n", is_power_of_two_popcount(b) ? "true" : "false");
    printf("recursive: %s\n", is_power_of_two_recursive(b) ? "true" : "false");

    return 0;
}


/*
OUTPUT:

Testing 16:
bitwise:   true
loop:      true
popcount:  true
recursive: true

Testing 18:
bitwise:   false
loop:      false
popcount:  false
recursive: false

*/

 



answered Apr 2 by avibootz
0 votes
#include <stdio.h>
#include <math.h>
#include <stdbool.h>

// Method A: Bit-counting
bool isPowerOfTwoA(int x) {
    if (x <= 0) return false;

    int count = 0;
    while (x > 0) {
        if (x & 1) count++;
        x >>= 1;
    }
    return count == 1;
}

// Method B: Bitwise trick
bool isPowerOfTwoB(int x) {
    return x > 0 && (x & (x - 1)) == 0;
}

// Method C: Repeated division
bool isPowerOfTwoC(int x) {
    if (x <= 0) return false;

    while (x % 2 == 0) {
        x /= 2;
    }
    return x == 1;
}

// Method D: Using logarithms
bool isPowerOfTwoD(int x) {
    if (x <= 0) return false;

    double logv = log(x) / log(2);
    return fabs(logv - round(logv)) < 1e-10;
}

int main() {
    int test1 = 16;  // true
    int test2 = 18;  // false

    printf("A: %s, %s\n", isPowerOfTwoA(test1) ? "true" : "false",
                           isPowerOfTwoA(test2) ? "true" : "false");

    printf("B: %s, %s\n", isPowerOfTwoB(test1) ? "true" : "false",
                           isPowerOfTwoB(test2) ? "true" : "false");

    printf("C: %s, %s\n", isPowerOfTwoC(test1) ? "true" : "false",
                           isPowerOfTwoC(test2) ? "true" : "false");

    printf("D: %s, %s\n", isPowerOfTwoD(test1) ? "true" : "false",
                           isPowerOfTwoD(test2) ? "true" : "false");
    
    return 0;
}


/*
OUTPUT:

A: true, false
B: true, false
C: true, false
D: true, false

*/

 



answered Apr 2 by avibootz

Related questions

...