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)
Answer Posted / 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 |
Post New Answer View All Answers
why nlogn is the lower limit of any sort algorithm?
write a program for area of circumference of shapes
What is data _null_? ,Explain with code when u need to use it in data step programming ?
Cluster head selection in Wireless Sensor Network using C programming language.
Design an implement of the inputs functions for event mode
What is full form of PEPSI
create a C-code that will display the total fare of a passenger of a taxi if the driver press enter,the timer will stop. Every 10 counts is 2 pesos. Initial value is 25.00
What is the match merge ? compare data step match merge with proc sql merge - how many types are there ? data step vs proc sql
How do you verify if the two sentences/phrases input is an anagram using predefined functions in string.h and by using arrays?
Sir... please give some important coding questions asked by product companies..
why do you use macros? Explain a situation where you had to incorporate macros in your proc report? use a simple instream data example with code ?
write a c program to input initial & final time in the format hh:mm and find the time intervel between them? Ex inputs are initial 06:30 final 00:05 and 23:22 final 22.30
I need your help, i need a Turbo C code for this problem.. hope u'll help me guys.? Your program will have a 3x3 array. The user will input the sum of each row and each column. Then the user will input 3 values and store them anywhere, or any location or index, temporarily in the array. Your program will supply the remaining six (6) values and determine the exact location of each value in the array. Example: Input: Sum of row 1: 6 Sum of row 2: 15 Sum of row 3: 24 Sum of column 1: 12 Sum of column 2: 15 Sum of column 3: 18 Value 1: 3 Value 2: 5 Value 3: 6 Output: Sum of Row 1 2 3 6 4 5 6 15 7 8 9 24 Sum of Column 12 15 18 Note: Your program will not necessary sort the walues in the array Thanks..
could you please send the program code for multiplying sparse matrix in c????
How can you relate the function with the structure? Explain with an appropriate example.