Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bit twiddling reorder

I need to do an arbitrary reorder of a 7 bit value (Yes I know I should be using a table) and am wondering if there are any bit hacks to do this.

Example:

// <b0, b1, b2, b3, b4, b5, b6> -> <b3, b2, b4, b1, b5, b0, b6>

// the naive way
out =
   (0x020 & In) << 5 |
   (0x008 & In) << 2 |
   (0x040 & In)      |
   (0x012 & In) >> 1 |
   (0x004 & In) >> 2 |
   (0x001 & In) >> 3;

// 6 ANDs, 5 ORs, 5 shifts = 16 ops

edit: I was thinking of something along the lines of this

Just for kicks and because I was AFTK I'm trying a brute force search for solutions of the form:

((In * C1) >> C2) & 0x7f

No solutions found.

like image 229
BCS Avatar asked Dec 08 '25 12:12

BCS


1 Answers

The first step seems to be to understand a mathematical solution and optimize that.

see here of bit hacks

like image 152
sfossen Avatar answered Dec 13 '25 19:12

sfossen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!