Wednesday, October 12, 2005

Amazed...

Sometimes you know what z happening by looking at the code, but never know why it has to be written that way...This piece of code is one such thing and it really amazed me today to learn why it needs to be written this way....

void send (int *to, int * from, int count)
{
int 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 );
}
}

Answer from wikipedia is here and the original mail about the inventor of this code is here . I have stopped my long time crib of needing break; in switch statements :) C is flawless !!!

2 Comments:

At 9:21 AM, Blogger Phoenix said...

Yep good old Duff's device...and I had been ignorant about it for long !

At first I thought it wont even compile....it took a lot of head scratching and recalling my fundas to convince myself that it will compile and to understand what z goin on.. :)

 
At 2:39 AM, Blogger Pal said...

Rule 1: C is flawless. Learn C.
Rule 2: Java is the Future.

PS: If Rule 2 is not true, perform Rule 1

 

Post a Comment

<< Home