Give a oneline C expression to test whether a number is a
power of 2?

Answers were Sorted based on User's Feedback

Give a oneline C expression to test whether a number is a power of 2? ..

if (x & (x-1)) // false only if x is a power of 2

 Is This Answer Correct ? 100 Yes 8 No

Give a oneline C expression to test whether a number is a power of 2? ..

# include <stdio.h>

void main()
{
int n;
scanf("%d",&n);
if( n & (n-1) )
printf("Not a Power of 2");
else
printf("Power of 2");
}

 Is This Answer Correct ? 47 Yes 5 No

Give a oneline C expression to test whether a number is a power of 2? ..

Previous post -- It's wrong... absolutely...

 Is This Answer Correct ? 15 Yes 2 No

Give a oneline C expression to test whether a number is a power of 2? ..

if (x && !(x & (x-1)) == 0)

 Is This Answer Correct ? 7 Yes 2 No

Give a oneline C expression to test whether a number is a power of 2? ..

(x ^ x-1) == ((x << 1) - 1)

 Is This Answer Correct ? 3 Yes 0 No

Give a oneline C expression to test whether a number is a power of 2? ..

I think
if (x & (x-1)) wont work when number is negative.

 Is This Answer Correct ? 12 Yes 10 No

Give a oneline C expression to test whether a number is a power of 2? ..

(~i & (i-1)) == (i - 1))

 Is This Answer Correct ? 2 Yes 0 No

Give a oneline C expression to test whether a number is a power of 2? ..

if (int(log(x)) == log(x)) // log is to the base 2

 Is This Answer Correct ? 1 Yes 0 No

Give a oneline C expression to test whether a number is a power of 2? ..

#define ISPOW2(x) ((x==1)?1:(x&(x-1)))?0:1

 Is This Answer Correct ? 1 Yes 0 No

Give a oneline C expression to test whether a number is a power of 2? ..

if ((num XOR (num - 1)) == num + num - 1) return true;
else return false;

 Is This Answer Correct ? 1 Yes 0 No

More C Code Interview Questions

main() { int i=10,j=20; j = i, j?(i,j)?i:j:j; printf("%d %d",i,j); }

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

#include<stdio.h> int main() { int x=2,y; y=++x*x++*++x; printf("%d",y); } Output for this program is 64. can you explain how this output is come??

main() { void swap(); int x=10,y=8; swap(&x,&y); printf("x=%d y=%d",x,y); } void swap(int *a, int *b) { *a ^= *b, *b ^= *a, *a ^= *b; }

#ifdef something int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); }

write a c program to print magic square of order n when n>3 and n is odd?

How can i find first 5 natural Numbers without using any loop in c language????????

Is it possible to type a name in command line without ant quotes?

void main() { int c; c=printf("Hello world"); printf("\n%d",c); }

what is variable length argument list?

main() { unsigned char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

How we print the table of 2 using for loop in c programing?

Categories
• C Code (422)
• C++ Code (127)
• VC++ Code (17)
• Java Code (84)
• Dot Net Code (114)
• Visual Basic Code (11)
• Programming Code AllOther (62)