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

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

3 Answers  


void ( * abc( int, void ( *def) () ) ) ();

1 Answers  


write a program to count the number the same (letter/character foreg: 's') in a given sentence.

2 Answers  


#include<stdio.h> main() { register i=5; char j[]= "hello"; printf("%s %d",j,i); }

2 Answers  


Extend the sutherland-hodgman clipping algorithm to clip three-dimensional planes against a regular paralleiepiped

1 Answers   IBM,






programming in c lanugaue programm will errror error with two header file one as stdio.h and other one is conio.h

1 Answers  


Write a routine that prints out a 2-D array in spiral order

3 Answers   Microsoft,


Which version do you prefer of the following two, 1) printf(ā€œ%sā€,str); // or the more curt one 2) printf(str);

1 Answers  


What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?

1 Answers  


A program that will create a movie seat reservation. The program will display the summary seats and its status. The user will be ask what seat no. to be reserved, then it will go back again to the summary to display the updated seat status. If the seat no. is already reserved then it will prompt an error message. And also if the input seat no is out of range then it will also prompt an error message. The program is continuously running. Termination of the program will depends on how the programmer will apply. Sample output: Movie Seats Reservation Summary of Seats: Seat 1: Available Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 1 Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 6 The Seat no. is out of range! Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 1 The Seat no. is already reserved! Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 0 GoodBye... Thank You!!!

0 Answers  


main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcat(a,b)); } a. Hello b. Hello World c. HelloWorld d. None of the above

3 Answers   HCL,


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

2 Answers  


Categories