Created
August 24, 2012 17:45
-
-
Save gtklocker/3453371 to your computer and use it in GitHub Desktop.
Z-order in 9 lines of C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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