Give a oneline C expression to test whether a number is a
power of 2?
Answers were Sorted based on User's Feedback
Answer / nmk
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
bool isPowerOf2(int number){
if ( number > 0 )
return (number & (number-1)) == 0;
if (number == 0)
return false;
if (isPowerOf2(-number)){
int count=0;
while ( !(number & 1)) {
++count;
number >>= 1;
}
if (fmod(count,2))
return true;
else
return false;
}
return false;
}
int main(void) {
for(int i=-1027; i<1025; ++i){
if (isPowerOf2(i) )
printf("%d\n", i);
}
return EXIT_SUCCESS;
}
| Is This Answer Correct ? | 2 Yes | 14 No |
Answer / carl menezes
#define ISPOW2(x) ( (x + (x-1) ) == (x<<1 + 1) )
| Is This Answer Correct ? | 10 Yes | 42 No |
Answer / sandhya
#include<stdio.h>
void main()
{
int number;
printf("|n enter a number");
scanf("%d",&number);
int twopower(int);
printf("\n the given number is ");
}
int twopower(int num)
{
while((num & -num)==num)
return(1);
printf("|n yes power of two");
}
| Is This Answer Correct ? | 9 Yes | 45 No |
Answer / mridul
#include<stdio.h>
main()
{
int a;
scanf("%d",&a);
if (a&1)
printf ("POWER off 2");
printf ("NOT power of 2");
}
| Is This Answer Correct ? | 3 Yes | 45 No |
main( ) { void *vp; char ch = ‘g’, *cp = “goofy”; int j = 20; vp = &ch; printf(“%c”, *(char *)vp); vp = &j; printf(“%d”,*(int *)vp); vp = cp; printf(“%s”,(char *)vp + 3); }
write a program in c language to get the value of arroy keys pressed and display the message which arrow key is pressed?
could you please send the program code for multiplying sparse matrix in c????
{ int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }
What is the output of the program given below main() { signed char i=0; for(;i>=0;i++) ; printf("%d\n",i); }
How to reverse a String without using C functions ?
33 Answers Matrix, TCS, Wipro,
Write a single line c expression to delete a,b,c from aabbcc
main() { { unsigned int bit=256; printf("%d", bit); } { unsigned int bit=512; printf("%d", bit); } } a. 256, 256 b. 512, 512 c. 256, 512 d. Compile error
struct Foo { char *pName; char *pAddress; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); obj->pName = malloc(100); obj->pAddress = malloc(100); strcpy(obj->pName,"Your Name"); strcpy(obj->pAddress, "Your Address"); free(obj); printf("%s", obj->pName); printf("%s", obj->pAddress); } a. Your Name, Your Address b. Your Address, Your Address c. Your Name Your Name d. None of the above
main() { extern out; printf("%d", out); } int out=100;
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
Write a complete program that consists of a function that can receive two numbers from a user (M and N) as a parameter. Then print all the numbers between the two numbers including the number itself. If the value of M is smaller than N, print the numbers in ascending flow. If the value of M is bigger than N, print the numbers in descending flow. may i know how the coding look like?