# ZX BASIC:HMirror.bas

# hMirror.bas

This Function takes a byte in, and returns the byte that has reflected the bits around the x axis, such that bits 7,6,5,4,3,2,1,0 become bits 0,1,2,3,4,5,6,7.

This can be useful if you elect to make all your graphics face in one direction - you can mirror the bytes (perhaps to a buffer or a UDG) before you print them. It's faster to store them facing both ways, but you can make quite a memory saving if you just choose one way.

FUNCTION FASTCALL hMirror (number AS UBYTE) AS UBYTE ASM ld c,a ; unrolled LOOP FOR speed. Still quite small - costs 10 bytes OVER the LOOP version, AND saves OVER half the time. ; 25 bytes AND 96 clock cycles RR C RLA RR C RLA RR C RLA RR C RLA RR C RLA RR C RLA RR C RLA RR C RLA END ASM END FUNCTION

The above function is basically deprecated, but may be easier to understand than the following. This one below is faster, and smaller. You should use this one:

FUNCTION FASTCALL hMirror (number AS UBYTE) AS UBYTE ASM ;17 bytes AND 66 clock cycles Reverse: ld b,a ;b=ABCDEFGH rrca ;a=HABCDEFG rrca ;a=GHABCDEF XOR b AND %10101010 XOR b ;a=GBADCFEH ld b,a ;b=GBADCFEH rrca ;a=HGBADCFE rrca ;a=EHGBADCF rrca ;a=FEHGBADC rrca ;a=CFEHGBAD XOR b AND %01100110 XOR b ;a=GFEDCBAH rrca ;a=HGFEDCBA END ASM END FUNCTION