Saturday 11 November 2017

C Program to Check if a given Integer is Power of 2 using Bitwise Operators


Code:

#include stdio.h
#define NUM_BITS_INT (8*sizeof(int))

int power_of_2(unsigned int);

int main()
{
unsigned int num;

printf("\nEnter Number");
scanf("%d", &num);
    power_of_2(num);
}

/*
 * Finding the power of 2 using bit wise operators
 */ 
int power_of_2(unsigned int x)
{
    int i, count = 0, result, shift_num;

    for (i = 0;i <= NUM_BITS_INT;i++)
    {
        shift_num = x >> i;
        result = shift_num & 1;
        if (res == 1)
            count++;
    }
    /*
     *If number of bits set to 1 are odd then the number is power of 2
     *If number of bits set to 0 are even then the number is not power of 2
     */
    if (count % 2 == 1)
        printf("YES");
    else 
        printf("NO");
}


Output:

Enter Number128
YES
$  a.out
Enter Number126
NO


More C Programs:














100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...