Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...

Give a one-line C expression to test whether a number is a
power of 2.

Answers were Sorted based on User's Feedback

Give a one-line C expression to test whether a number is a power of 2...

void main()
{
int a;
scanf("%d",&a);

if((a&a-1)==0)
printf("Is a power of 2");
else
printf("Not a power of 2");
}

 Is This Answer Correct ? 32 Yes 8 No

Give a one-line C expression to test whether a number is a power of 2...

if(x&(x-1)==0)
then TRUE;
else
FALSE;

use the bitwise and operator

 Is This Answer Correct ? 21 Yes 2 No

Give a one-line C expression to test whether a number is a power of 2...

#include<stdio.h>
main()
{
int num;
printf("Enter Number
");
scanf("%d",&num);
(num & num - 1)? printf("Not
"):printf("Power of two
");
}

 Is This Answer Correct ? 1 Yes 0 No

Give a one-line C expression to test whether a number is a power of 2...

if( (a == 2 ) || ((a/2) %2 == 0 ) && a !=1)
TRUE
else
false

 Is This Answer Correct ? 1 Yes 2 No

Give a one-line C expression to test whether a number is a power of 2...

if((log(n)/log(2))/floor(log(n)/log(2))==1)
then TRUE;
else FALSE;

 Is This Answer Correct ? 1 Yes 5 No

Give a one-line C expression to test whether a number is a power of 2...

#include<stdio.h>
main()
{
int n,i,d,m;
printf("Enter a number");
scanf("%d",&n);
m=n;
while(n>0)
{
d=n%10;
if(d!=0)
{
prntf("%d is not power of 2",m);
getch();
exit();
}
n=n/10;
}
prntf("%d is power of 2",m);
getch();
}

 Is This Answer Correct ? 0 Yes 5 No

Give a one-line C expression to test whether a number is a power of 2...

#include<iostream.h>
int main()
{
int n,i;
cout<<"Enter a number";
cin>>n;
for(i=1;i<n/2;i++)
{
if((2<<i)==n)
{
cout<<"The given no is power of 2";
break;
}
}
}

 Is This Answer Correct ? 3 Yes 9 No

Give a one-line C expression to test whether a number is a power of 2...

#include<conio.h>
void main()
{
int n,i=0,num=0;
printf("\n enter any number");
scanf("%d",&n);
while(num<=n)
{
i++;
num=2^i;
}
if(num==n)
printf("yes no. is power of 2");
else
printf("no.");
}

 Is This Answer Correct ? 0 Yes 6 No

Give a one-line C expression to test whether a number is a power of 2...

/*following expr evaluates to true if num is a power of
2.Else it's false. '&' - bitwise and.*/
(num == 1) || !(num & 1)

 Is This Answer Correct ? 0 Yes 9 No

Give a one-line C expression to test whether a number is a power of 2...

#include<stdio.h>
void main()
{
int i,j;
clrscr();
printf("\n Enter a num");
scanf("%d",&i);
j=i;
for(;i%2==0;i/=2);
if(i==1)
printf("\n%d is power of 2",j);
else
printf("\n%d is not a power of 2");
getch();
}

 Is This Answer Correct ? 4 Yes 18 No

More C Code Interview Questions

Write a program to print a square of size 5 by using the character S.

# include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); }

Write a procedure to implement highlight as a blinking operation

#define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }

String copy logic in one line.

Write a program that produces these three columns sequence nos. using loop statement Sequence nos. Squared Squared + 5 1 1 6 2 4 9 3 9 14 4 16 21 5 25 30

How we will connect multiple client ? (without using fork,thread)

how can i cast a char type array to an int type array

main() { char *p = “ayqm”; printf(“%c”,++*(p++)); }

what is variable length argument list?

How do you verify if the two sentences/phrases input is an anagram using predefined functions in string.h and by using arrays?

struct Foo { char *pName; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); strcpy(obj->pName,"Your Name"); printf("%s", obj->pName); } a. Your Name b. compile error c. Name d. Runtime error

Categories