Go to Post I think one of they beauties of the program is how it can include so many different interests our students have. - Doc Wu [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 10-02-2011, 15:09
Robototes2412's Avatar
Robototes2412 Robototes2412 is offline
1 * 4 != 14
FRC #2412 (Robototes)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2007
Location: Bellevue
Posts: 312
Robototes2412 is on a distinguished road
Better bitwise conversion

Hello,

I am working on a program to control an arduino from a ds, and I have this code that converts all of the button input bools to a byte.

Code:
				//pack buttons into a byte
				if(keysHeld() & KEY_A) {
				    amount += 1;
				} else if (keysHeld() & KEY_B) {
				    amount += 2;
				} else if (keysHeld() & KEY_Y) {
				    amount += 4;
				} else if (keysHeld() & KEY_X) {
				    amount += 8;
				} else if (keysHeld() & KEY_L) {
				    amount += 16;
				} else if (keysHeld() & KEY_R) {
				    amount += 32;
				} else if (keysHeld() & KEY_START) {
				    amount += 64;
				} else if (keysHeld() & KEY_SELECT) {
				    amount += 128;
				} else {
				    amount = 0;
				}
amount is a byte.

How can i do this more elegantly?
Reply With Quote
  #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
  #3   Spotlight this post!  
Unread 10-02-2011, 20:19
Robototes2412's Avatar
Robototes2412 Robototes2412 is offline
1 * 4 != 14
FRC #2412 (Robototes)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2007
Location: Bellevue
Posts: 312
Robototes2412 is on a distinguished road
Re: Better bitwise conversion

its for a ds interface to an arduino robot my team is working on
Reply With Quote
  #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
  #5   Spotlight this post!  
Unread 10-02-2011, 21:21
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

Quote:
Originally Posted by dbeckwith View Post
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.
But this for-loop is copying the exact same bit pattern from keys to packedKeys. So what's the difference between that and this?
Code:
packedKeys = keys;
__________________
Reply With Quote
  #6   Spotlight this post!  
Unread 10-02-2011, 21:59
demosthenes2k8's Avatar
demosthenes2k8 demosthenes2k8 is online now
Graduated but not gone
AKA: Matt Soucy
FRC #0166 (Chop Shop 166)
Team Role: Mentor
 
Join Date: Jan 2009
Rookie Year: 2007
Location: Merrimack, NH
Posts: 589
demosthenes2k8 is a splendid one to beholddemosthenes2k8 is a splendid one to beholddemosthenes2k8 is a splendid one to beholddemosthenes2k8 is a splendid one to beholddemosthenes2k8 is a splendid one to beholddemosthenes2k8 is a splendid one to beholddemosthenes2k8 is a splendid one to beholddemosthenes2k8 is a splendid one to behold
Send a message via AIM to demosthenes2k8 Send a message via Yahoo to demosthenes2k8
Re: Better bitwise conversion

keys stores them differently, according to this:
Code:
typedef enum KEYPAD_BITS {
  KEY_A      = BIT(0),  //!< Keypad A button.
  KEY_B      = BIT(1),  //!< Keypad B button.
  KEY_SELECT = BIT(2),  //!< Keypad SELECT button.
  KEY_START  = BIT(3),  //!< Keypad START button.
  KEY_RIGHT  = BIT(4),  //!< Keypad RIGHT button.
  KEY_LEFT   = BIT(5),  //!< Keypad LEFT button.
  KEY_UP     = BIT(6),  //!< Keypad UP button.
  KEY_DOWN   = BIT(7),  //!< Keypad DOWN button.
  KEY_R      = BIT(8),  //!< Right shoulder button.
  KEY_L      = BIT(9),  //!< Left shoulder button.
  KEY_X      = BIT(10), //!< Keypad X button.
  KEY_Y      = BIT(11), //!< Keypad Y button.
  KEY_TOUCH  = BIT(12), //!< Touchscreen pendown.
  KEY_LID    = BIT(13)  //!< Lid state.
} KEYPAD_BITS;
__________________


GSR Dean's List Finalist 2011
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 13:06.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi