Skip to content

Instantly share code, notes, and snippets.

@pdxjohnny
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdxjohnny/e1f7527ec4a533252586 to your computer and use it in GitHub Desktop.
Save pdxjohnny/e1f7527ec4a533252586 to your computer and use it in GitHub Desktop.
C Shifting bits in a byte
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