Hornpipe2 Posted May 1, 2008 Share Posted May 1, 2008 (edited) 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 May 1, 2008 by Hornpipe2 Quote Link to comment Share on other sites More sharing options...
+batari Posted May 1, 2008 Share Posted May 1, 2008 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? Quote Link to comment Share on other sites More sharing options...
Hornpipe2 Posted May 1, 2008 Author Share Posted May 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.