Jump to content
IGNORED

Swap 2 Bits


Recommended Posts

Simple 6502 challenge: I have joystick code that is dependent on the player's facing, so when they turn around, I need to swap the L and R inputs.

 

Can you think of an easy way to swap the position of two bits? E.g. this sample:

 

00001011

would become

00001101

 

and

00001110

would become

00001110

 

EDIT: You can do this as an exercise if you like, but I think a better solution for me is just to AND with 00001100 and then XOR the result with the original value. This flips L/R and turns 00 into 11, which in my movement table will be the same effect.

Edited by Hornpipe2
Link to comment
Share on other sites

Simple 6502 challenge: I have joystick code that is dependent on the player's facing, so when they turn around, I need to swap the L and R inputs.

 

Can you think of an easy way to swap the position of two bits? E.g. this sample:

 

00001011

would become

00001101

 

and

00001110

would become

00001110

 

EDIT: You can do this as an exercise if you like, but I think a better solution for me is just to AND with 00001100 and then XOR the result with the original value. This flips L/R and turns 00 into 11, which in my movement table will be the same effect.

Your solution won't work, at least not for the conditions you describe. It will turn 01 into 10 and vice-versa, but will turn 11 into 00 and keep 00 unchanged.

 

You only have 16 possible values there, so why not just use a lookup table?

Link to comment
Share on other sites

EDIT: You can do this as an exercise if you like, but I think a better solution for me is just to AND with 00001100 and then XOR the result with the original value. This flips L/R and turns 00 into 11, which in my movement table will be the same effect.

Your solution won't work, at least not for the conditions you describe. It will turn 01 into 10 and vice-versa, but will turn 11 into 00 and keep 00 unchanged.

 

You only have 16 possible values there, so why not just use a lookup table?

Sorry I wasn't clear... the lookup table is exactly the solution I will be using. However after thinking about it the AND is unnecessary, I'll save a couple bytes without it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...