Page **1** of **2**

### [DM16] 8 bit reversal? Easiest way?

Posted: **Thu Aug 06, 2020 1:16 pm**

by **Mr_Plant**

I have a lot of 8 bit binary bytes to reverse like so

10110111 would become 11101101

Been trying to think of the best way to do this on the dm16

My Programming Skills are lacking in it but it’s still on my

Mind. Just wondered if anyone had seen such a routine as a quick search came up blank..

### Re: 8 bit reversal? Easiest way?

Posted: **Thu Aug 06, 2020 2:26 pm**

by **dlachieze**

Something like that:

Code: Select all

```
001 LBL A
002 HEX
003 8
004 STO I
005 CLX
006 LBL 0
007 X<>Y
008 RRC
009 X<>Y
010 RLC
011 DSZ
012 GTO 0
013 BIN
014 RTN
```

At step 13 put the mode you want to have at the end of the routine.

### Re: 8 bit reversal? Easiest way?

Posted: **Thu Aug 06, 2020 7:21 pm**

by **Mr_Plant**

Fantastic ! Thanks it was the technique I was after- I’ll adapt that to exactly what I want . I actually wrote code for The Arduino project I’m writing to do it today but I really want the calculator to do it for the next batch of data I have and to have it as a stored program will be a brilliant help! The calculator rotates bits and converts brilliantly just needed to add this bit reversal ability to it - handy for flipping 8 bit graphics data left to right !

### Re: 8 bit reversal? Easiest way?

Posted: **Fri Aug 07, 2020 11:35 am**

by **pauli**

How about this alternative? It doesn't involve a loop but requires the WSIZE to be at least 42:

Code: Select all

```
001 LBL A
002 HEX
003 2
004 0
005 2
006 0
007 2
008 0
009 2
010 0
011 2
012 *
013 1
014 0
015 8
016 8
017 4
018 4
019 2
020 2
021 0
022 1
023 0
024 AND
025 3
026 F
027 F
028 RMD
029 RTN
```

It's twice as long.

Pauli

### Re: 8 bit reversal? Easiest way?

Posted: **Fri Aug 07, 2020 2:46 pm**

by **Mr_Plant**

X<>Y above - do you mean key program code 43.00 or 34 ?

Tried code but it just ran endlessly .. doing something wrong here I think..

### Re: 8 bit reversal? Easiest way?

Posted: **Fri Aug 07, 2020 3:03 pm**

by **dlachieze**

Here is the program with the key codes:

Code: Select all

```
001 43,22,A LBL A
002 23 HEX
003 8 8
004 44 32 STO I
005 43 35 CLX
006 43,22,0 LBL 0
007 34 X<>Y
008 43 d RRC
009 34 X<>Y
010 43 C RLC
011 43 23 DSZ
012 22 0 GTO 0
013 26 BIN
014 43 21 RTN
```

### Re: 8 bit reversal? Easiest way?

Posted: **Fri Aug 07, 2020 5:03 pm**

by **dlachieze**

pauli wrote: ↑Fri Aug 07, 2020 11:35 am

How about this alternative? It doesn't involve a loop but requires the WSIZE to be 64:

Very interesting, it's longer but much faster. However I can't figure out how it works. May you provide some explanation ?

### Re: 8 bit reversal? Easiest way?

Posted: **Fri Aug 07, 2020 10:42 pm**

by **Mr_Plant**

Same here can’t work it out but it’s very clever !

### Re: 8 bit reversal? Easiest way?

Posted: **Sat Aug 08, 2020 11:07 am**

by **pauli**

This second version does three steps (values are in hexadecimal):

- Multiply by 202020202 which copies the byte five times across 41 bits.
- AND with 10884422010 which selects the bits in the correct positions based on
*ten* bit bytes.
- Remainder 3FF (= 2^10-1 ) brings everything together.

It is helpful to look at this in binary assuming the bits in the byte are labelled

*a*,

*b*,

*c*,

*d*,

*e*,

*f*,

*g* and

*h* respectively:

Code: Select all

```
abcdefgh
10 00000010 00000010 00000010 00000010 *
----------------------------------------------
0 00000000 00000000 00000000 0000000a bcdefgh0 The all zero lines are omitted for brevity.
0 00000000 00000000 0000000a bcdefgh0 00000000
0 00000000 0000000a bcdefgh0 00000000 00000000
0 0000000a bcdefgh0 00000000 00000000 00000000
a bcdefgh0 00000000 00000000 00000000 00000000 +
----------------------------------------------
a bcdefgha bcdefgha bcdefgha bcdefgha bcdefgh0 Multiplication result.
1 00001000 10000100 01000010 00100000 00010000 AND
----------------------------------------------
a 0000f000 b0000g00 0c0000h0 00d00000 000e0000 AND result.
a 0000f000b0 000g000c00 00h000d000 00000e0000 Regrouping into units of ten bits.
1111111111 RMD
---------------------------------------------
00hgfedcba
```

The 64 bit word size isn't mandatory, 41 bits are sufficient. However, use 42 bits for signed calculations.

There is a whole family of algorithms like this where a longer multiplication is used to rearrange bits and optionally add them together.

Pauli

### Re: [DM16] 8 bit reversal? Easiest way?

Posted: **Sun Aug 09, 2020 9:32 am**

by **Mr_Plant**

Brilliant!