intlliner.blogg.se

0xde 0xad 0xbe 0xef 0xfe 0xed
0xde 0xad 0xbe 0xef 0xfe 0xed






0xde 0xad 0xbe 0xef 0xfe 0xed

Unsigned char inByte3 = (toReverse & 0xFF000000) > 24 Unsigned char inByte2 = (toReverse & 0xFF0000) > 16 Unsigned char inByte1 = (toReverse & 0xFF00) > 8 Unsigned char inByte0 = (toReverse & 0xFF)

0xde 0xad 0xbe 0xef 0xfe 0xed

If you want to do this on a 32-bit int, just reverse the bits in each byte, and reverse the order of the bytes.

0xde 0xad 0xbe 0xef 0xfe 0xed

Simple unsigned int v // input bits to be reversed unsigned int r = v & 1 // r will be reversed bits of v first get LSB of v int s = sizeof(v) * CHAR_BIT - 1 // extra shift needed at end for (v >= 1 v v >= 1)įaster (64-bit processor) unsigned char b // reverse this (8-bit) byteī = (b * 0x0202020202ULL & 0x010884422010ULL) % 1023 You can extend this idea to 64-bit ints, or trade off memory for speed (assuming your L1 Data Cache is large enough), and reverse 16 bits at a time with a 64K-entry lookup table. Unsigned char * q = ( unsigned char *) &c Option 2: unsigned char * p = ( unsigned char *) &v Low Memory (32-bit int, 32-bit machine)(from here): unsigned int reverse ( register unsigned int x)

#0xde 0xad 0xbe 0xef 0xfe 0xed portable#

NOTE: All algorithms below are in C, but should be portable to your language of choice (just don’t look at me when they’re not as fast 🙂 Options








0xde 0xad 0xbe 0xef 0xfe 0xed