Last active
August 29, 2015 14:20
-
-
Save pdxjohnny/e1f7527ec4a533252586 to your computer and use it in GitHub Desktop.
C Shifting bits in a byte
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
char swap_bits( char byte, int one, int two ) | |
{ | |
char tmp_1, tmp_2, swap; | |
int hex_1 = pow(2, one); | |
int hex_2 = pow(2, two); | |
tmp_1 = byte & hex_1; | |
tmp_1 = tmp_1 >> abs( one - two ); | |
tmp_2 = byte & hex_2; | |
tmp_2 = tmp_2 << abs( one - two ); | |
if ( tmp_1 == 0 && tmp_2 != 0 ) | |
{ | |
byte = byte ^ hex_2; | |
} | |
if ( tmp_2 == 0 && tmp_1 != 0 ) | |
{ | |
byte = byte ^ hex_1; | |
} | |
swap = tmp_1 | tmp_2; | |
byte = swap | byte; | |
return byte; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment