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

Write a program using one dimensional array to assign values and then display it on the screen. Use the formula a[i]=i*10 to assign value to an element.

1 Answers   Samar State University,


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

7 Answers  


What is your nationality?

1 Answers   GoDB Tech,


How to read a directory in a C program?

4 Answers  


void func1(int (*a)[10]) { printf("Ok it works"); } void func2(int a[][10]) { printf("Will this work?"); } main() { int a[10][10]; func1(a); func2(a); } a. Ok it works b. Will this work? c. Ok it worksWill this work? d. None of the above

1 Answers   HCL,






main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }

1 Answers  


There were 10 records stored in “somefile.dat” but the following program printed 11 names. What went wrong? void main() { struct student { char name[30], rollno[6]; }stud; FILE *fp = fopen(“somefile.dat”,”r”); while(!feof(fp)) { fread(&stud, sizeof(stud), 1 , fp); puts(stud.name); } }

1 Answers  


Who could write how to find a prime number in dynamic array?

1 Answers  


void main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } }

1 Answers  


main() { int i = 258; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }

1 Answers  


Write a program to implement the motion of a bouncing ball using a downward gravitational force and a ground-plane friction force. Initially the ball is to be projected in to space with a given velocity vector

2 Answers  


void main() { if(~0 == (unsigned int)-1) printf(“You can answer this if you know how values are represented in memory”); }

1 Answers  


Categories