Go Back   EQEmulator Home > EQEmulator Forums > Development > Development::Development

Development::Development Forum for development topics and for those interested in EQEMu development. (Not a support forum)

Reply
 
Thread Tools Display Modes
  #1  
Old 10-23-2008, 06:45 PM
seveianrex
Sarnak
 
Join Date: Sep 2008
Location: asdf
Posts: 60
Default Generic Coding Question

Okay, admittedly I'm a self taught programmer and I never quite understood binary operators.

In working with the "CLASSES" column for AA's I was able to get a grasp on the binary 'AND' (&) operator, and I understand how bitshifting (1 << value) works.


I was curious though... I've seen some very elegant looking code before in terms of something like the following:

Code:
switch (GetAA(aaNumberOne) | GetAA(aaNumberTwo))
{
//blah
}
which would utilize the non-zero value.

would the above code work? if aaNum1 returned 0 and aaNum2 returned 3, for example, would it switch (3) ?

just seems a lot more elegant than doing two switches that do the same thing. i realize i could use an integer variable too, but that gets clunky lookin'.
Reply With Quote
  #2  
Old 10-24-2008, 12:20 PM
Sildorei
Fire Beetle
 
Join Date: Oct 2008
Location: Toulouse
Posts: 1
Default

No, it wouldn't work because in a switch you'll only use a code like following :
Code:
switch(myvar)
You can't use a binary operator in a switch ( i think).
__________________
Reply With Quote
  #3  
Old 10-24-2008, 12:56 PM
spoon
Sarnak
 
Join Date: Aug 2007
Posts: 34
Default

Actually it should work because bitwise operations are expressions that return an integral type. An expression doesn't have to be just a variable.

VS2005 switch definition

And our quick test proves it:

Code:
#include <iostream>
using namespace std;

int main() {
	int x = 1;
	int y = 5;
	
	switch( x | y ){
		case 0: printf("0"); break;
		case 1: printf("1"); break;
		case 2: printf("2"); break;
		case 3: printf("3"); break;
		case 4: printf("4"); break;
		case 5: printf("5"); break;
		case 6: printf("6"); break;
		case 7: printf("7"); break;
		default: printf("other"); break;
	}
}
Code:
5
Reply With Quote
  #4  
Old 10-24-2008, 01:31 PM
Derision
Developer
 
Join Date: Feb 2004
Location: UK
Posts: 1,540
Default

You're fine if one of the two operands is always zero, but you may get strange results if both are non-zero, e.g. 1 | 3 = 3, 1 | 4 = 5.
Reply With Quote
  #5  
Old 10-24-2008, 02:32 PM
spoon
Sarnak
 
Join Date: Aug 2007
Posts: 34
Default

I would bet he is asking because of the duplicate AAs where one would always be 0.

However, I don't think this is a "elegant" solution, but a clever one. Clever meaning buggy and hard to maintain. At this level of abstraction, we are dealing with int32s and not bits or bitmasks. You could do something like:
Code:
int32 aaValue = ( GetAA(aaNumberOne) > GetAA(aaNumberTwo) )  ? GetAA(aaNumberOne) : GetAA(aaNumberTwo);

case(aaValue) {
... snip ...
if you wanted to be clever, but still maintain the proper level of abstraction. However I would write the extra couple lines and maybe add a comment just to let the maintainer know whats going on.

I tend to follow the KISS principle (Keep It Simple Stupid)
Reply With Quote
  #6  
Old 10-24-2008, 02:41 PM
spoon
Sarnak
 
Join Date: Aug 2007
Posts: 34
Default

Quote:
Originally Posted by spoon View Post
Code:
case(aaValue) {
Code:
switch(aaValue) {
Reply With Quote
Reply


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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

   

All times are GMT -4. The time now is 05:20 PM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3