View Single Post
  #2   Spotlight this post!  
Unread 10-02-2011, 17:13
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: Better bitwise conversion

Is there a reason why you are translating one bit position to another? It looks like keysHeld() is already returning a bit per key. Can't you just do the following?
Code:
amount = keysHeld();
The only difference is keysHeld() can return more than one bit set and "amount" in your code can only have one bit set.
If you want to isolate one bit from all the set bits returned by keysHeld(), you can do this:
Code:
int rawBits = keysHeld();
int amount = rawBits & ~(rawBits ^ -rawBits);
Or is keysHeld() returning more than one byte (e.g. 16-bit) and you are trying to compress it into one byte? If so you may do the following:
Code:
int rawBits = keysHeld();
byte amount = 0;
 
if (rawBits & KEY_A) amount |= 0x01;
if (rawBits & KEY_B) amount |= 0x02;
if (rawBits & KEY_Y) amount |= 0x04;
if (rawBits & KEY_X) amount |= 0x08;
if (rawBits & KEY_L) amount |= 0x10;
if (rawBits & KEY_R) amount |= 0x20;
if (rawBits & KEY_START) amount |= 0x40;
if (rawBits & KEY_SELECT) amount |= 0x80;
Just curious, these looks like xbox controller buttons, what are you trying to do?
__________________

Last edited by mikets : 10-02-2011 at 17:26.
Reply With Quote