News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu
avatar_redbox

Swapping bits in a byte

Started by redbox, 22:43, 17 February 13

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

redbox

I have a byte loaded into A as follows:

LD A,(variable)

And I want to swap the following bits X and Y over in the byte - for example:

128 64 32 16 8 4 2 1
0   0  X  X  X Y Y Y

becomes

128 64 32 16 8 4 2 1
0   0  Y  Y  Y X X X

Any ideas?
All registers are available and ideally the result will end up in A.
No tables please, so I realise it'll probably be a combination of bit shifting (RRA etc) and OR and AND but can't think of the fastest way...?!

Grim

Maybe something along the lines of:

; A = %00XXXYYY
add a,a
add a,a
; A = %XXXYYY00
add a,a
ld c,a
; C = A = %XXYYY000, Carry X
adc a,a
adc a,a
adc a,a
; A = %YY000XXX, Carry Y
add a,c
and %00111111
; A = %00YYYXXX
; 10µs

TFM

Nice idea!! A RRCA approach wouldn't be more quick here :)
TFM of FutureSoft
Also visit the CPC and Plus users favorite OS: FutureOS - The Revolution on CPC6128 and 6128Plus

redbox

Quote from: Grim on 00:00, 18 February 13
Maybe something along the lines of:

Thanks Grim, that's great.

Nice use of the carry as a buffer.

Powered by SMFPacks Menu Editor Mod