do something-1
I am taking a course this semester called Advanced operating systems. My professor who is an industry veteran has this habit of taking attendance by giving a short code reading test at the beginning of every class. The idea is actually not about attendance , but we get some exposure to complex and some of the best codes. The test involves some small C code which does something and we need to figure out what that code does in 5 minutes. Some of these codes are best ways to do something. Just have a look at the following code.
Explanation in the comments :-)
#include <stdio.h>
#include <stdlib.h>
unsigned long do_something(unsigned n){
int i;
for(i=0;n!=0;n&=n-1)
++i;
return i;
}
Explanation in the comments :-)
Labels: best c codes, code reading, do something

1 Comments:
The code actually counts the number of ones in the binary representation of the number.
Post a Comment
Subscribe to Post Comments [Atom]
<< Home