What is the most efficient way to count the number of bits
which are set in a value?

Answers were Sorted based on User's Feedback



What is the most efficient way to count the number of bits which are set in a value?..

Answer / venkat1435

main()
{
int n,count=0;
printf("enter a number");
scanf("%d",&n);//enterd no.is 5
while(n>0)
{
count++;
n=n&n-1;//binary of n is 101
// binary of n-1 is 100
//n&n-1 is (i.e 101
&100 =100 )

}
printf("%d",count);
getch();
} output is 2(i.e 2 ones in 101)

Is This Answer Correct ?    31 Yes 6 No

What is the most efficient way to count the number of bits which are set in a value?..

Answer / pappu kumar sharma

int fnCntbts(int num )
{
int iCnt = 0;
while ( num )
{
num &= (num-1) ;
iCnt++;
}

return iCnt;

}

Is This Answer Correct ?    11 Yes 2 No

What is the most efficient way to count the number of bits which are set in a value?..

Answer / boomer

pseudo

int size is 32 bits...right?

for(i = 0 to 31)
{
count += (value & 1) //& = and oprator
shift left value
}

Is This Answer Correct ?    6 Yes 5 No

What is the most efficient way to count the number of bits which are set in a value?..

Answer / bryan w

Question needs clarification. What platform? There are
bitwise trick that are optimal for various platforms, but
you need to know if the value is 16 bit, 32 bit, 64 bit, 128
bit, or something else entirely.

The earlier three examples are all incorrect for operating
on signed integers; if a negative value is presented the
code will fail.

The most efficient portable human readable answer is to loop
over sizeof type * CHAR_BITS times, shift left and add one
if the bit is set.

On some platforms the most efficient way is to use a bit of
assembly (such as the POPCNT instruction if available) to
perform the operation for you.

Without either of those, there are simple classic algorithms
of &, |, and ^ to accumulate the bits and sum them. They
need to be adjusted to match the architecture's number of
bits. These routines may be inefficient on modern PCs with
long pipelines or out-of-order processing cores.

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More C Interview Questions

What is main function in c?

0 Answers  


Write a program to print this triangle: * ** * **** * ****** * ******** * ********** Don't use printf statements;use two nested loops instead. you will have to use braces around the body of the outer loop if it contains multiple statements.

6 Answers   Wipro,


In cryptography, you could often break the algorithm if you know what was the original (plain) text that was encoded into the current ciphertext. This is called the plain text attack. In this simple problem, we illustrate the plain text attack on a simple substitution cipher encryption, where you know each letter has been substituted with a different letter from the alphabet but you don’t know what that letter is. You are given the cipherText as the input string to the function getwordSets(). You know that a plain text "AMMUNITION" occurs somewhere in this cipher text. Now, you have to find out which sets of characters corresponds to the encrypted form of the "AMMUNITION". You can assume that the encryption follows simple substitution only. [Hint: You could use the pattern in the "AMMUNITION" like MM occurring twice together to identify this]

0 Answers   Infosys,


What is a structural principle?

0 Answers  


Write down the program to sort the array.

4 Answers   Impiger,


what is the output of the following program? #include<stdio.h> void main() { float x=1.1; while(x==1.1) { printf("\n%f",x); x=x-0.1; } }

6 Answers  


without using arithmatic operator convert an intger variable x into x+1

3 Answers  


Are there any problems with performing mathematical operations on different variable types?

0 Answers  


A SIMPLE PROGRAM OF GRAPHICS AND THEIR OUTPUT I WANT SEE WAHAT OUTOUT OF GRAPHICS PROGRAM

0 Answers   HCL,


How can I change their mode to binary?

0 Answers  


what would be the output of the following program main() { int a[] = {1,2,3,4,5}; int *ptr = {a,a+1,a+2,a+3,a+4}; printf("%d %d %d %d",a,*ptr,**ptr,ptr); } }

1 Answers  


any function have arguments one or more OR not . it is compulsary a) any function compulsary have one or more arguments b) any function did not have arguments. It is not compulsary c) it is optional it is not compulsary d) none of the above

0 Answers  


Categories