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

Contributed software for the DM10, DM11, DM12, DM15 and DM16 goes here.

Please prefix the subject of your post with the model of the calculator that your program is for.
Mr_Plant
Posts: 13
Joined: Fri Mar 06, 2020 4:27 am

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

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..
dlachieze
Posts: 263
Joined: Thu May 04, 2017 12:20 pm
Location: France

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

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.
DM42: 00425 - DM41X: β00066
Mr_Plant
Posts: 13
Joined: Fri Mar 06, 2020 4:27 am

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

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 !
pauli
Posts: 122
Joined: Tue May 02, 2017 10:11 am
Location: Australia

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

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
Last edited by pauli on Sat Aug 08, 2020 11:53 am, edited 1 time in total.
Mr_Plant
Posts: 13
Joined: Fri Mar 06, 2020 4:27 am

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

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..
dlachieze
Posts: 263
Joined: Thu May 04, 2017 12:20 pm
Location: France

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

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

DM42: 00425 - DM41X: β00066
dlachieze
Posts: 263
Joined: Thu May 04, 2017 12:20 pm
Location: France

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

pauli wrote:
Fri Aug 07, 2020 11:35 am
Very interesting, it's longer but much faster. However I can't figure out how it works. May you provide some explanation ?
DM42: 00425 - DM41X: β00066
Mr_Plant
Posts: 13
Joined: Fri Mar 06, 2020 4:27 am

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

Same here can’t work it out but it’s very clever !
pauli
Posts: 122
Joined: Tue May 02, 2017 10:11 am
Location: Australia

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

This second version does three steps (values are in hexadecimal):
1. Multiply by 202020202 which copies the byte five times across 41 bits.
2. AND with 10884422010 which selects the bits in the correct positions based on ten bit bytes.
3. 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
Mr_Plant
Posts: 13
Joined: Fri Mar 06, 2020 4:27 am

Brilliant!