Saturday 11 November 2017

C Program to Check if All the Bits of a given Integer is One(1)


Code:

#include stdio.h>

int all_bits_one(int);
int count = 0;

void main()
{
    int num;
    printf("enter the number:");
    scanf("%d", &num);
    num++;
    all_bits_one(num);
    if (count)
    {
        printf("false");
    }
    else
    {
        printf("true");
    }
}

/* checks whether all bits are 1 */
int all_bits_one(int x)
{    
    if (x == 1)
        return 0;
    if (x % 2 != 0)
    {
        count++;
    }
    else
    {
        x = x / 2;
        all_bits_one(x);
    }
}


Output:

enter the number:5
false
$ a.out
enter the number:7
true
$ a.out
enter the number:127
true
$ ./a.out
enter the number:125
false

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...