Rediscovering Hamming Code
blog.digital-horror.com
blog.digital-horror.com
#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.
So the original user's comment might infact get detected by this and get optimised down to using popcnt. Will need to try it out. :-)