Home > OS >  How to function that will test if numbers are power of 2?
How to function that will test if numbers are power of 2?

Time:02-28

I already made program, for one number to test if that number is power of 2. But I have problem, and I don't know how to fix it. But I need to make program that will test more then just one number. I don't know if I explained that good (because I don't know english very well). Do you have any suggestions, what can I change in code ?

#include <stdio.h>

//function prototype for checking power of two
int checkPowerofTwo(int n);

int main()
{
   int num;

   printf("Enter the number you want to test: ");
   scanf("%d", &num);

   if (checkPowerofTwo(num) == 1)
      printf("\n%d is a power of 2\n", num);
   else
      printf("\n%d is not a power of 2\n", num);

   return 0;
}

//function body
int checkPowerofTwo(int x)
{
   //checks whether a number is zero or not
   if (x == 0)
      return 0;

   //true till x is not equal to 1
   while( x != 1)
   {
      //checks whether a number is divisible by 2
      if(x % 2 != 0)
         return 0;
         x /= 2;
   }
   return 1;
}

CodePudding user response:

If I have understood correctly what you need is a loop. For example

int main( void )
{
    while ( 1 )
    { 
       int num;

       printf("Enter the number you want to test (0 - exit ): ");

       if ( scanf("%d", &num) != 1 || num == 0 ) break;

       if ( checkPowerofTwo( num ) )
          printf("\n%d is a power of 2\n", num);
       else
          printf("\n%d is not a power of 2\n", num);
    }

   return 0;
}

Pay attention to that your function will not work correctly if the user will enter a negative number.

So it is better to declare the variable num and the function parameter as having unsigned type unsigned int

CodePudding user response:

How about something like this

for (int i = 0; i<1000; i  ) {
       if (checkPowerofTwo(i) == 1)
            printf("\n%d is a power of 2\n", i);
   }

to test the code?

CodePudding user response:

How to function that will test if numbers are power of 2?

For non-negative integers, there's a trick to do this very quickly:

    if( x & (x - 1) == 0) {
        // Either zero or a power of 2
    }

To also support negative integers you can:

    if(x > 0) {
        if( x & (x - 1) == 0) {
            // It's a power of 2
        }
    } else if(x < 0) {
        if( -x & (-x - 1) == 0) {
            // It's a power of 2
        }
   }

Note that your original code doesn't support negative numbers (and you probably should be using unsigned int everywhere, including using scanf("%u", &num);).

But I need to make program that will test more then just one number.

That's a different problem:

int main() {
    unsigned int num;

    do {
        printf("Enter the positive number you want to test, or 0 to exit: ");
        scanf("%u", &num);
        if(num == 0) {
            return 0;
        }

        if (checkPowerofTwo(num) == 1) {
            printf("\n%u is a power of 2\n", num);
        } else {
            printf("\n%u is not a power of 2\n", num);
        }
    } while(1);   // Loop forever
}

CodePudding user response:

Thank you all. The only thing I did is changing int checkPowerofTwo(int x) to int checkPowerofTwo(int num), changed every intiger x to num and now it works.

  • Related