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

How will you print % character? a. printf(“\%”) b. printf(“\\%”) c. printf(“%%”) d. printf(“\%%”)

4 Answers   HCL,


main() { 41printf("%p",main); }8

1 Answers  


main ( ) { static char *s[ ] = {“black”, “white”, “yellow”, “violet”}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; printf(“%s”,*--*++p + 3); }

1 Answers  


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

1 Answers   Zoho,


Display the time of the system and display the right time of the other country

1 Answers  






write a function to give demostrate the functionality of 3d in 1d. function prototye: change(int value,int indexX,int indexY,int indexZ, int [] 1dArray); value=what is the date; indexX=x-asix indexY=y-axis indexZ=z-axis and 1dArray=in which and where the value is stored??

0 Answers   Nagarro,


pls anyone can help me to write a code to print the values in words for any value.Example:1034 to print as "one thousand and thirty four only"

2 Answers  


#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }

1 Answers  


Given a spherical surface, write bump-mapping procedure to generate the bumpy surface of an orange

0 Answers  


int i,j; for(i=0;i<=10;i++) { j+=5; assert(i<5); }

3 Answers   Cisco, HCL,


main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); }

3 Answers  


main() { extern int i; i=20; printf("%d",i); }

1 Answers   Value Labs,


Categories