Skip to content

Instantly share code, notes, and snippets.

@aneury1
Created February 2, 2025 10:28
Show Gist options
  • Save aneury1/2da360d1a279a4b44fa48c30a6d7c0a0 to your computer and use it in GitHub Desktop.
Save aneury1/2da360d1a279a4b44fa48c30a6d7c0a0 to your computer and use it in GitHub Desktop.
// ReverseByte.cpp
uint8_t reverse_byte(uint8_t b){
unsigned int v = b;
unsigned int r = v;
int s = sizeof(b) * CHAR_BIT - 1;
for(v>>=1;v;v>>=1){
r <<= 1;
r |= v&1;
s--;
}
r <<= s;
return r;
}
int main(){
auto ik = 0x0F;
auto ki = reverse_byte(ik);
std::cout << std::hex <<(int)ki <<"\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment