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
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 |
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 |
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 |
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 |
while running a program, i got the msg that press return key to exit.what that mean in C as there are no such options as far i know.
who is the editor of 'pokemon'?
Write a C Program to display the following menu: Menu 1. Display 2. Copy 3. Append 4. Exit Accept the choice (1-4) from the user, and perform the following tasks: Choice 1: Accept a file name from the user and display the file on screen Choice 2: Accept two file names, and copy first file to the second Choice 3: Accept two file names, and append second file to the first file Choice 4: Terminate the program
1 Answers Accenture, Concor, DMU, Satyam, Syntel, Tora,
Display names and numbers of employees who have 5 years or more experience and salary less than Rs.15000 using array of structures (name, number, experience and salary)
What is the difference between File pointer and Internal Charecter Pointer?
Write a C program where input is: "My name is xyz". output is: "xyz is name My".
c program to input values in a table(using 2D array) and print odd numbers from them
read the folllowing code # define MAX 100 # define MIN 100 .... .... if(x>MAX) x=1; else if(x<MIN) x=-1; x=50; if the initial value of x=200,what is the vlaue after executing this code? a.200 b.1 c.-1 d.50
What are the primitive data types in c?
write a c/c++ programthat connects to a MYSQL server and checks if the INNoDB plug in is installed on it.If so your program should print the total number of disk writes by MYSQL.
Why enum is used in c?
helllo sir , what is the main use of the pointer ,array ,and the structure with the example of a programe