Spit bit

I need to do an arbitrary reordering of a 7-bit value (yes, I know that I have to use a table), and I am wondering if there are any bit hacks for 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 like this

Just for strokes and because I was AFTK, I try to find brute force for form decisions:

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

No solutions found.

+3
source share
4 answers

"" , . - , (V++ 2005) , , , , "(0x001 In) → 3".

, , , , .

V++ 2005....

"", , :

mov eax,469h

. .

... :

00401D4F  mov         eax,ecx 
00401D51  and         eax,20h 
00401D54  shl         eax,3 
00401D57  mov         edx,ecx 
00401D59  and         edx,8 
00401D5C  or          eax,edx 
00401D5E  mov         edx,ecx 
00401D60  sar         edx,1 
00401D62  and         edx,2 
00401D65  or          edx,ecx 
00401D67  sar         edx,1 
00401D69  shl         eax,2 
00401D6C  and         edx,9 
00401D6F  or          eax,edx 
00401D71  and         ecx,40h 
00401D74  or          eax,ecx 

, , OR - . , , .

, , , .: -)

+4

, , .

. -

+4

- , 32- ( 10 , AND OR, AFAICR).

, , .

:)

+2

, "" , . :

for (b=0;b<7;b++)
{
    i=1<<b;
    printf("%d: %02x -> %02x\n", b, i, shuffle(i));
}

, . , .

0: 01 -> 00
1: 02 -> 01
2: 04 -> 01
3: 08 -> 20
4: 10 -> 08
5: 20 -> 00
6: 40 -> 40

, :

   //    0 1 2 3 4 5 6 
   //-> 3 2 4 1 5 0 6
   (0x001 & In) << 3 |
   (0x004 & In) << 2 |
   (0x020 & In)      |
   (0x012 & In) << 1 |
   (0x008 & In) >> 2 |
   (0x020 & In) >> 5 ;
0

Source: https://habr.com/ru/post/1704417/


All Articles