Shift Operations

Q

What will the following piece of code do?

int f(unsigned int x) {
   int i;
   for (i=0; x!=0; x>>1) {
      if (x & 0x1)
      i++;
   }
   return i;
}

✍: FYIcenter

A

Answer: returns the number of ones in the input parameter X.

2007-02-26, 5567👍, 0💬