Add reverse_byte()

This commit is contained in:
Nathael Pajani 2020-11-19 19:48:38 +01:00
parent 04fb4d4fc7
commit 6c09a47ce4
1 changed files with 23 additions and 0 deletions

View File

@ -100,3 +100,26 @@ uint8_t bits_set(uint32_t x)
return r;
}
#if 0
/*
* Reverse bits (order) in a single byte
*/
uint8_t reverse_byte(uint8_t b) {
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}
/*
* Another method to reverse bits order, using a lookup table
*/
uint8_t reverse_byte(uint8_t b) {
static const uint8_t rev_half[] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf, };
/* Reverse the top and bottom nibble then swap them. */
return (rev_half[b & 0x0F] << 4) | rev_half[b >> 4];
}
#endif