Check if a number is a power of 2

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <unistd.h>

#include <math.h>

int main(int argc, char *argv[])

{

int X,i;

 while(1)

 {

  printf("Input X: ");

  scanf("%d",&X);

  //if((X & (X-1)) == 0) 

  if((X | (X-1)) != 0)

  {

   i = log2(X);

   printf("%d is 2 to the %d power!\n",X,i);

  }

  else

  {

   printf("%d is not a power of 2!\n",X);

  }

 }

 return 0;

}

Guess you like

Origin blog.csdn.net/qq_72714790/article/details/126459092