Tuesday, April 23, 2013

Binary Operations for Combining Multiple Selections

My latest realization: using XOR on a binary number can be used for combining menu selections.  Explanation below!

1:       int *coords;  
2:       int linecount=0;  
3:       short int missionTemp=1, missionChoice=0;  
4:       while(missionTemp!=0)  
5:       {  
6:            cout << "Enter mission desired, or 0 to continue: " << endl;  
7:            cout << "1. Fondor Escort (Lvl 10ish)" << endl;  
8:            cout << "2. Javaal Fleet Action (Lvl 10ish)" << endl;  
9:            cout << "3. Balosar Outpost (Lvl 10ish)" << endl;  
10:            cout << "4. Makem Te Assault (Lvl 20)" << endl;  
11:            cout << "5. Archenar Interception (Lvl 20)" << endl;  
12:            cout << "6. Syvris Evacuation (Lvl 28)" << endl;  
13:            cout << "7. Llanic Station Strike (Lvl 28)" << endl;  
14:            cout << "8. Paakuni Defense (Lvl 34)" << endl;  
15:            cin >> missionTemp;  
16:            if(missionTemp==1)  
17:                 missionChoice=missionChoice^1;  
18:            else if (missionTemp==2)  
19:                 missionChoice=missionChoice^2;  
20:            else if (missionTemp==3)  
21:                 missionChoice=missionChoice^4;  
22:            else if (missionTemp==4)  
23:                 missionChoice=missionChoice^8;  
24:            else if (missionTemp==5)  
25:                 missionChoice=missionChoice^16;  
26:            else if (missionTemp==6)  
27:                 missionChoice=missionChoice^32;  
28:            else if (missionTemp==7)  
29:                 missionChoice=missionChoice^64;  
30:            else if (missionTemp==8)  
31:                 missionChoice=missionChoice^128;  
32:            else if (missionTemp==0)  
33:                 break;  
34:            else  
35:                 break;  
36:       }  
37:       cout << "MissionChoice=" << missionChoice << endl;  
38:       cout << "Please switch to SWTOR and position properly." << endl;  
39:       Sleep(2000);  
40:       while(1)  
41:       {  
42:            if((missionChoice&1)==1)  
43:            {  
44:                 coords=readFile(fondor.inFile, linecount);  
45:                 while(1)  
46:                 {  
47:                      doMission(fondor.submapX, fondor.submapY, fondor.missionX, fondor.missionY, fondor.greenValue, linecount, coords);  
48:                 }  
49:            }  
50:            if((missionChoice&2)!=0)  
51:            {  
52:                 coords=readFile(javaal.inFile, linecount);  
53:                 while(1)  
54:                 {  
55:                      doMission(javaal.submapX, javaal.submapY, javaal.missionX, javaal.missionY, javaal.greenValue, linecount, coords);  
56:                 }  
57:            }  
58:            if((missionChoice&4)!=0)  
59:            {  
60:                 coords=readFile(balosar.inFile, linecount);  
61:                 while(1)  
62:                 {  
63:                      doMission(balosar.submapX, balosar.submapY, balosar.missionX, balosar.missionY, balosar.greenValue, linecount, coords);  
64:                 }  
65:            }  
66:            if((missionChoice&8)!=0)  
67:            {  
68:                 coords=readFile(makem.inFile, linecount);  
69:                 while(1)  
70:                 {  
71:                      doMission(makem.submapX, makem.submapY, makem.missionX, makem.missionY, makem.greenValue, linecount, coords);  
72:                 }  
73:            }  
74:            if((missionChoice&16)!=0)  
75:            {  
76:                 coords=readFile(archenar.inFile, linecount);  
77:                 while(1)  
78:                 {  
79:                      doMission(archenar.submapX, archenar.submapY, archenar.missionX, archenar.missionY, archenar.greenValue, linecount, coords);  
80:                 }  
81:            }  
82:            if((missionChoice&32)!=0)  
83:            {  
84:                 coords=readFile(syvris.inFile, linecount);  
85:                 while(1)  
86:                 {  
87:                      doMission(syvris.submapX, syvris.submapY, syvris.missionX, syvris.missionY, syvris.greenValue, linecount, coords);  
88:                 }  
89:            }  
90:            if((missionChoice&64)!=0)  
91:            {  
92:                 coords=readFile(llanic.inFile, linecount);  
93:                 while(1)  
94:                 {  
95:                      doMission(llanic.submapX, llanic.submapY, llanic.missionX, llanic.missionY, llanic.greenValue, linecount, coords);  
96:                 }  
97:            }  
98:            if((missionChoice&128)!=0)  
99:            {  
100:                 coords=readFile(pakuuni.inFile, linecount);  
101:                 while(1)  
102:                 {  
103:                      doMission(pakuuni.submapX, pakuuni.submapY, pakuuni.missionX, pakuuni.missionY, pakuuni.greenValue, linecount, coords);  
104:                 }  
105:            }  
106:       }  
107:  }  

I wanted my bot to run a customized selection of missions repeatedly, instead of having to compile a separate executable for every mission, or only allowing one choice.

Using binary, it's easier to see the operation in action.

If the user selects 1, 2, and 3, these are the binary operations that take place:

 missionChoice=00000000 (initial value)  
           XOR 00000001 (selecting 1 XORs with 1)
 missionChoice=00000001 (end result)  

missionChoice=00000001 (initial value)  
          XOR 00000010 (selecting 2 XORs with 2)
missionChoice=00000011 (end result) 

missionChoice=00000011 (initial value) 
         XOR  00000100 (selecting 3 XORs with 4)
missionChoice=00000111 (end result)  

After using the XOR process to combine the selections into a single variable, an AND operation is used with the same numbers to check if that value was previously selected:

 
missionChoice=00000111 (initial value)  
          AND 00000001 (was "1" selected?)  
              00000001 (anything other than 0 means it was selected)
  
missionChoice=00000111 (initial value)  
          AND 00000010 (was "2" selected)  
              00000010 (once again, non-0 value means it was selected)
  
missionChoice=00000111 (initial value)  
          AND 00000100 (was "3" selected)  
              00000100 (etc etc)  

Cool stuff!

No comments:

Post a Comment