Periodically while my mind wanders I alight upon Duff's device, one of the cleverest pieces of C code I have ever seen. (Apparently Stroustrup thinks so as well; he included it in his book). I understand perfectly well what the code does, but somehow it still seems elusive every time I see it.
send(to, from, count) register short *to, *from; register count; { register n=(count+7)/8; switch(count%8){ case 0: do{ *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; }while(--n>0); } }
One of the best interview questions I have seen is a slightly obfuscated version of Duff's device with the variable names replaced and all comments unhelpfully removed. The questioner then asks a job applicant, What does this function do? Why? Familiarity with the literature is a great help here.
No comments:
Post a Comment