View Single Post
  #4   Spotlight this post!  
Unread 10-02-2011, 20:32
dbeckwith's Avatar
dbeckwith dbeckwith is offline
Lead Programmer
AKA: Daniel Beckwith
FRC #3205 (The Patriots)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2009
Location: USA
Posts: 84
dbeckwith is an unknown quantity at this point
Re: Better bitwise conversion

Well one way you could do it is with a for loop, but that would only really be efficient if the bit masks are consecutive. I only know Java, so I might get the C++ syntax wrong, but here is what it might look like:
Code:
int keys = keysHeld();
byte packedKeys = 0;

for (int i = 0; i < 8; i++) {
  if (keys & (1 << i)) packedKeys |= 1 << i;
}
That would pack the values of the keys, but only if the bit masks were all in a line. You could also start the i variable at the first mask if it isn't zero. I have the same question as mikets though. Why do you even need to do this if keysHeld() already returns the key values? It seems redundant to me.
__________________
q = (2*b) | ~(2*b);

if (life.getLemons() != null) this.lemonade = new Drink(life.getLemons());
else throw new NoLemonsException("What now?");


Reply With Quote