Author Topic: Swapping bits in a byte  (Read 1091 times)

0 Members and 1 Guest are viewing this topic.

Offline redbox

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.788
  • Country: gb
    • redbox
  • Liked: 406
  • Likes Given: 287
Swapping bits in a byte
« on: 23:43, 17 February 13 »
I have a byte loaded into A as follows:
Code: [Select]
LD A,(variable)
And I want to swap the following bits X and Y over in the byte - for example:
Code: [Select]
128 64 32 16 8 4 2 1
0   0  X  X  X Y Y Y
becomes
Code: [Select]
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...?!
« Last Edit: 00:06, 18 February 13 by redbox »

Offline Grim

  • CPC6128
  • ****
  • Posts: 202
  • Country: gp
  • La pak ba, bèf ka pasé
    • THERE IS NO GAME
  • Liked: 134
  • Likes Given: 67
Re: Swapping bits in a byte
« Reply #1 on: 01:00, 18 February 13 »
Maybe something along the lines of:
Code: [Select]
; 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

Offline TFM

  • Visit the mysteries of the CPC at www.futureos.de
  • Supporter
  • 6128 Plus
  • *
  • Posts: 9.899
  • Country: aq
  • Space Chicken for FutureOS is free!
    • index.php?action=treasury
    • FutureOS - The revolution on CPC!
  • Liked: 1985
  • Likes Given: 4650
Re: Swapping bits in a byte
« Reply #2 on: 01:12, 18 February 13 »
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

Offline redbox

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.788
  • Country: gb
    • redbox
  • Liked: 406
  • Likes Given: 287
Re: Swapping bits in a byte
« Reply #3 on: 11:55, 18 February 13 »
Maybe something along the lines of:

Thanks Grim, that's great.

Nice use of the carry as a buffer.