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

Answer / dan

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

Is This Answer Correct ?    102 Yes 8 No

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

Answer / vadivel

# 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 ?    48 Yes 5 No

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

Answer / ataraxic

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

Answer / phil

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

Answer / jb

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

Answer / ashutosh

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

Answer / natmat

(~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? ..

Answer / fjords

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

Answer / rahul priyadarshi

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

Answer / yevgen

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

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More C Code Interview Questions

what is the output of following program ? void main() { int i=5; printf("%d %d %d %d %d ",i++,i--,++i,--i,i); }

10 Answers  


#define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); }

1 Answers  


write a c program to input initial & final time in the format hh:mm and find the time intervel between them? Ex inputs are initial 06:30 final 00:05 and 23:22 final 22.30

0 Answers  


How do I write a program to print proper subset of given string . Eg :input: abc output:{},{a},{b},{c},{a,b},{a,c},{b,c}, {a,b,c}.I desperately need this program please mail me to saravana6m@gmail.com

11 Answers   Deshaw, Infosys,


struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); }

1 Answers  






1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we replace the value of the j then anser is different?why? 2)int i=5; printf("%d",i++ + i++ + i++); this givs 18.

8 Answers   IBPS, Infosys, TCS,


main() { int i=5; printf("%d",++i++); }

1 Answers  


write the function. if all the character in string B appear in string A, return true, otherwise return false.

11 Answers   Google,


Write a single line c expression to delete a,b,c from aabbcc

2 Answers   Microsoft,


How to reverse a String without using C functions ?

33 Answers   Matrix, TCS, Wipro,


main() { show(); } void show() { printf("I'm the greatest"); }

2 Answers  


1. const char *a; 2. char* const a; 3. char const *a; -Differentiate the above declarations.

3 Answers  


Categories