Powers of 2 are closly linked to the position values of binary numbers. The most right position is 2 to the power 0 = 1 then goes to the left with 2 to the power 1 = 2, 2 to the power 2 = 4 and such. If you want to know that a number fits this sequence, you can use this litle code.

```
/*
find out of an integer entered is a power of 2
*/
#include<stdio.h>
int main(void)
{
int n, z1;
char c[2][30]={"is not a power of 2","is a power of 2"};
printf("Enter integer to find out if power of 2: ");
scanf("%d",&n);
/* test */
z1 = (n & (n-1));
if ((n & (n-1))==0) printf("\ntrue\n");
printf("\n%d %s\n",n, c[((n & (n-1))==0) ? 1 : 0]);
getchar(); /* catch enter */
getchar(); /* wait */
return 0;
}
```

About the Author