|
|
|
|
|
by anonymousiam
1892 days ago
|
|
Re: "fast" parity code in article. I always preferred the parity example from the (pre-ansi) K&R C Programming Language book: #include <stdio.h> main( argc, argv )
int argc;
char *argv[]; { unsigned source;
int parity;
if ( argc > 1 )
{
if ( sscanf( argv[1], "%d", &source ) == 1 )
{
printf( "%d = ", source );
for ( parity = 0; source; parity++ )
source &= ( source - 1 );
printf( "%d\n", parity & 1 );
}
}
}The function in the article is faster for > 8-bit values when there are more than eight ones in the value being checked. Also, I suppose theirs is better than K&R because it always executes in the same number of cycles. |
|
I think the reason why I chose that particular one was to show how thinking about a problem from an entirely different angle can yield great (and rewarding) results.