Skip to content

Instantly share code, notes, and snippets.

@gtklocker
Created August 24, 2012 17:45
Show Gist options
  • Save gtklocker/3453371 to your computer and use it in GitHub Desktop.
Save gtklocker/3453371 to your computer and use it in GitHub Desktop.
Z-order in 9 lines of C
// It only z-orders 8-bit numbers correctly but you can modify
// the for loop so that it works for more bits (maybe replace 8
// with 16 or 32, 64 is probably a bad idea unless you have a
// 128-bit system) and the return/ret and parameter type accordingly.
int zOrder( int lhs, int rhs ) {
int bit, ret;
for ( ret = 0, bit = 0; bit < 8; ++bit ) {
ret |= ( ( ( lhs & 1 ) << 1 ) | ( rhs & 1 ) ) << ( bit << 1 );
lhs >>= 1;
rhs >>= 1;
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment