For latest Software Job Openings and Recruitment in India and Abroad, Freshers and Experienced, check at the bottom of the page
Factorial using Recursive Function
int Factorial(int number)
{
if(number == 0)
{
return 1;
}
return (number * Factorial(number - 1));
}
int main()
{
cout<<”Factorial of 7 is \t”<< Factorial(7);
}
Thanks to Sean Eron Anderson (http://graphics.stanford.edu/~seander/bithacks.html) for the following programs.
Determining if an integer is a power of 2
unsigned int v; // we want to see if v is a power of 2 bool f; // the result goes here f = (v & (v - 1)) == 0; Note that 0 is incorrectly considered a power of 2 here. To remedy this, use: f = v && !(v & (v - 1));
Counting bits set (naive way)
unsigned int v; // count the number of bits set in v
unsigned int c; // c accumulates the total bits set in v
for (c = 0; v; v >>= 1)
{
c += v & 1;
}


