• Source
    1. #include <stdio.h>
    2.  
    3. int IsPowerTwo( unsigned int temp )
    4. {
    5. int count = 0;
    6. int nbitset = 0;
    7. int bit = 0;
    8.  
    9. while( count < 32 )
    10. {
    11. bit = ( temp >> count ) & 0x01ul;
    12. if(bit)
    13. {
    14. nbitset++;
    15. if( nbitset > 1 )
    16. {
    17. return 0;
    18. }
    19. }
    20. count++;
    21. }
    22.  
    23. if( nbitset == 1)
    24. return 1;
    25. else
    26. return 0;
    27.  
    28. }
    29.  
    30. int main(void) {
    31.  
    32. int num = 16;
    33. if( IsPowerTwo(num) )
    34. {
    35. printf("the number %d is power of 2 \n", num);
    36. }
    37. else
    38. {
    39. printf("the number %d is not power of 2 \n", num);
    40. }
    41.  
    42. // your code goes here
    43. return 0;
    44. }
    45.