reverse bits / reverse bytes

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

reverse bits / reverse bytes

10 Aug 2017, 18:52

I was curious as to whether any libraries have been written to handle the reordering of bits and bytes.

Although I could myself write such functions, the difficulties lie in the vast range of possibilities in writing each function, questions over optimisation, and various choices in writing the functions, including whether to combine various functions into one. Hence I raise the problem here.

I had in mind to apply these functions to integers stored in variables rather than to binary data.

REVERSE ALL BITS:
UChar: abcdefgh -> hgfedcba
UShort: abcdefgh ijklmnop -> ponmlkji hgfedcba
UInt: abcdefgh ijklmnop qrstuvwx yzABCDEF -> FEDCBAzy xwvutsrq ponmlkji hgfedcba

REVERSE BITS WITHIN BYTES:
UChar: abcdefgh -> hgfedcba
UShort: abcdefgh ijklmnop -> hgfedcba ponmlkji
UInt: abcdefgh ijklmnop qrstuvwx yzABCDEF -> hgfedcba ponmlkji xwvutsrq FEDCBAzy

REVERSE BYTES:
UShort: [ab -> ba] abcdefgh ijklmnop -> ijklmnop abcdefgh
UInt: [abcd -> dcba] abcdefgh ijklmnop qrstuvwx yzABCDEF -> yzABCDEF qrstuvwx ijklmnop abcdefgh

==================================================

SOME EXAMPLES:

There are not written for AutoHotkey, but can be converted quite easily.

Code: Select all

In C/C++ what's the simplest way to reverse the order of bits in a byte? - Stack Overflow
https://stackoverflow.com/questions/2602823/in-c-c-whats-the-simplest-way-to-reverse-the-order-of-bits-in-a-byte

unsigned char reverse(unsigned char b) {
   b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
   b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
   b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
   return b;
}

notes:
0xF0 = 0b11110000, 0x0F = 0b00001111
abcd     ->     abcd
    efgh -> efgh
abcdefgh -> efghabcd

0xCC = 0b11001100, 0x33 = 0b00110011
ef  ab   ->   ef  ab
  gh  cd -> gh  cd
efghabcd -> ghefcdab

0xAA = 0b10101010, 0x55 = 0b01010101
g e c a  ->  g e c a
 h f d b -> h f d b
ghefcdab -> hgfedcba

==================================================

crc.c.txt
http://www.hackersdelight.org/hdcodetxt/crc.c.txt

// Reverses (reflects) bits in a 32-bit word.
unsigned reverse(unsigned x) {
   x = ((x & 0x55555555) <<  1) | ((x >>  1) & 0x55555555);
   x = ((x & 0x33333333) <<  2) | ((x >>  2) & 0x33333333);
   x = ((x & 0x0F0F0F0F) <<  4) | ((x >>  4) & 0x0F0F0F0F);
   x = (x << 24) | ((x & 0xFF00) << 8) |
       ((x >> 8) & 0xFF00) | (x >> 24);
   return x;
}

notes:
0x55555555 = 0b01010101010101010101010101010101
0x33333333 = 0b00110011001100110011001100110011
0x0F0F0F0F = 0b00001111000011110000111100001111
0xFF00 = 0b1111111100000000
==================================================

LINKS:

[reverse bits]
crc.c.txt
http://www.hackersdelight.org/hdcodetxt/crc.c.txt
In C/C++ what's the simplest way to reverse the order of bits in a byte? - Stack Overflow
https://stackoverflow.com/questions/260 ... -in-a-byte
Best Algorithm for Bit Reversal ( from MSB->LSB to LSB->MSB) in C - Stack Overflow
https://stackoverflow.com/questions/746 ... b-msb-in-c
c# - Is there a built-in function to reverse bit order - Stack Overflow
https://stackoverflow.com/questions/358 ... -bit-order
C reverse binary - Stack Overflow
https://stackoverflow.com/questions/129 ... rse-binary
binary - C reverse bits in unsigned integer - Stack Overflow
https://stackoverflow.com/questions/914 ... ed-integer
c - Bit reversal of an integer, ignoring integer size and endianness - Stack Overflow
https://stackoverflow.com/questions/637 ... endianness

[reverse bytes]
c# - Bitwise endian swap for various types - Stack Overflow
https://stackoverflow.com/questions/195 ... ious-types
c - How to reverse byte of a hexadecimal number? - Stack Overflow
https://stackoverflow.com/questions/210 ... mal-number
swap - convert big endian to little endian in C [without using provided func] - Stack Overflow
https://stackoverflow.com/questions/218 ... vided-func

[also]
Bit Twiddling Hacks
http://graphics.stanford.edu/~seander/bithacks.html
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], CarPiotr, erikals, k4rkarin, Kellyzkorner_NJ, Xtra and 39 guests