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 |
void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); }
Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal.
6 Answers Fusion Systems GmbH,
how can u draw a rectangle in C
53 Answers Accenture, CO, Codeblocks, Cognizant, HCL, Oracle, Punjab National Bank, SAP Labs, TCS, University, Wipro,
main() { char not; not=!2; printf("%d",not); }
#include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); }
int a=1; printf("%d %d %d",a++,a++,a); need o/p in 'c' and what explanation too
main() { int i =10, j = 20; clrscr(); printf("%d, %d, ", j-- , --i); printf("%d, %d ", j++ , ++i); } a. 20, 10, 20, 10 b. 20, 9, 20, 10 c. 20, 9, 19, 10 d. 19, 9, 20, 10
Is there any difference between the two declarations, 1. int foo(int *arr[]) and 2. int foo(int *arr[2])
Write a C program that defines a 2-dimentional integer array called A [50][50]. Then the elements of this array should randomly be initialized either to 1 or 0. The program should then print out all the elements in the diagonal (i.e. a[0][0], a[1][1],a[2][2], a[3][3], ……..a[49][49]). Finally, print out how many zeros and ones in the diagonal.
plz send me all data structure related programs
main(int argc, char *argv[]) { (main && argc) ? main(argc-1, NULL) : return 0; } a. Runtime error. b. Compile error. Illegal syntax c. Gets into Infinite loop d. None of the above
main() { int i=10,j=20; j = i, j?(i,j)?i:j:j; printf("%d %d",i,j); }