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 ?    100 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 ?    47 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

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

2 Answers   CSC, Adobe,


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

0 Answers  


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

1 Answers  


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; }

2 Answers  


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

1 Answers  






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

1 Answers   HCL,


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

2 Answers   Microsoft,


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

1 Answers   Excel, Infosys,


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

2 Answers  


what is variable length argument list?

2 Answers  


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

1 Answers  


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

14 Answers   Wipro, HCL,






Categories