From 6c09a47ce436531cb5d60a86b9f3dd812907697f Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Thu, 19 Nov 2020 19:48:38 +0100 Subject: [PATCH] Add reverse_byte() --- lib/utils.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/utils.c b/lib/utils.c index 96ab676..5b02434 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -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 + -- 2.43.0