Give a very good method to count the number of ones in a 32
bit number.
(caution: looping through testing each bit is not a solution)
Answers were Sorted based on User's Feedback
Answer / vijay
#include<stdio.h>
main()
{
int x=123,i=0;
while(x)
{
x=x&(x-1);
i++;
}
printf("Number of set bits are %d \n",i);
}
Is This Answer Correct ? | 24 Yes | 9 No |
Answer / xyz
main()
{
int i=1177;
int j=0;
while(i>0)
{
if((i%2)!=0)
j++;
i=i/2;
}
printf("The number of one is %d\n", j);
}
Is This Answer Correct ? | 14 Yes | 6 No |
Answer / lawrence
@Turk
your solution is O(n) not O(logn). your algorithm still
counts each zeros and ones.
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / sunny arora
/* This is a table which will contain number of ones
corrosponding to number value */
static int bits_in_char [256] ;
int bitcount (unsigned int n)
{
// works only for 32-bit ints
return bits_in_char [n & 0xffu]
+ bits_in_char [(n >> 8) & 0xffu]
+ bits_in_char [(n >> 16) & 0xffu]
+ bits_in_char [(n >> 24) & 0xffu] ;
}
Is This Answer Correct ? | 9 Yes | 9 No |
Answer / turk
Here is an O(logn) solution
#include<stdio.h>
int numberOfOnesByte(unsigned char c, int length){
if (c==0)
return 0;
if (c==1)
return 1;
unsigned char left, right;
int lengthP = length/2;
left = c>>lengthP;
right = c-(left<<lengthP);
return numberOfOnesByte(left,length-lengthP)+numberOfOnesByte(right,lengthP);
}
int numberOfOnes(unsigned char *array, int start, int end, int length){
if(length==1){
return numberOfOnesByte(array[start],8);
} else {
int lengthP = length/2;
return numberOfOnes(array,start,start+lengthP-1,lengthP)+
numberOfOnes(array,start+lengthP,end,length-lengthP);
}
}
int main(){
unsigned char array[8] = {0xFF,0XAA,0xFF,0XAA,0xFF,0XAA,0xFF,0XAA};
printf("number of bits %d\n",numberOfOnes(array,0,7,8));
return 0;
}
Is This Answer Correct ? | 3 Yes | 5 No |
Answer / pgmrsf
static void NumOfOnes(uint n)
{
int c = 0;
for (int i=0; i<10; i++)
{
if ((n & (int)Math.Pow(2, i)) == (int)Math.Pow(2,
i))
{
c++;
}
}
Console.WriteLine("{0}", c);
}
Is This Answer Correct ? | 4 Yes | 7 No |
Write a Program that Inputs 10 Numbers in an Array and Show the Maximum Number
#define SQR(x) x * x main() { printf("%d", 225/SQR(15)); } a. 1 b. 225 c. 15 d. none of the above
what will be the output of this program? void main() { int a[]={5,10,15}; int i=0,num; num=a[++i] + ++i +(++i); printf("%d",num); }
writte a c-programm to display smill paces
abcdedcba abc cba ab ba a a
How can u say that a given point is in a triangle? 1. with the co-ordinates of the 3 vertices specified. 2. with only the co-ordinates of the top vertex given.
Design an implement of the inputs functions for event mode
How to access command-line arguments?
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
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
#include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->name); }
#define a 10 int main() { printf("%d..",a); foo(); printf("%d..",a); return 0; } void foo() { #undef a #define a 50 }