Programming Algorithms Question:

Counting set bits in a number

Tweet Share WhatsApp

Answer:

First version:

int CoutSetBits(int Num)
{

for(int count=0; Num; Num >>= 1)
{
if (Num & 1)
count++;
}
return count;
}


Optimized version:

int CoutSetBits(int Num)
{

for(int count =0; Num; count++)
{
Num &= Num -1;
}
}

Download Programming Algorithms PDF Read All 30 Programming Algorithms Questions
Previous QuestionNext Question
Return Nth the node from the end of the linked list in one pass.Define and state the importance of sub algorithm in computation and its relation ship with main algorithm?