View Single Post
  #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