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)) 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'. |
No, it wouldn't work because in a switch you'll only use a code like following :
Code:
switch(myvar) |
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> Code:
5 |
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.
|
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); I tend to follow the KISS principle (Keep It Simple Stupid) |
Quote:
Code:
switch(aaValue) { |
All times are GMT -4. The time now is 09:59 PM. |
Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.