Hello Daniel and all Dev-C++ users,
Saturday, April 26, 2003, 5:34:24 PM, Daniel wrote as follows:
DG> Hi, I'm using the Half Life SDK and am confused by the ampersand symbol, can
DG> anyone explain what's happening please?
DG> inline BOOL HasConditions( int iConditions ) { if ( m_afConditions &
DG> iConditions ) return TRUE; return FALSE; }
DG> I also notice we have return TRUE; return FALSE; I'm assuming this is
DG> perfectly valid as in
DG> if (condition)
DG> return TRUE;
DG> // if condition was true we would have exited by now.
DG> return FALSE;
DG> This isn;t relating to HL, it's just a general question about the
DG> conditional statement. Is it something to do with bit masking?
Yeah, look at this simple sample (I used something very similar in
PHP):
unsigned int uiTypeA = 1; //bits: 00000001
unsigned int uiTypeB = 2; //bits: 00000010
unsigned int uiTypeC = 4; //bits: 00000100
unsigned int uiTypeD = 8; //bits: 00001000
unsigned int uiTypeAC = uiTypeA | uiTypeC; // == 5,bits: 00000101
and now checking:
//uiTepeNew
if( uiTypeNew & uiTypeA )//is bit 00000001 set??
;//do something here
if( uiTypeNew & uiTypeB )//is bit 00000010 set??
;//do something here
if( uiTypeNew & uiTypeA and uiTypeNew & uiTypeB )//are bits one and two set??
;//do some...
and this is something very different:
if( uiTypeNew & uiTypeAB )
;//do some...
which means if one (or both) of uiTypeA or uiTypeB set?? This is like
logical OR, and equal to:
if( uiTypeNew & uiTypeA or uiTypeNew & uiTypeB )
this bit feauture might be used to send few options (type yes/no) in
one variable... sometimes VERY USEFUL - use only if needed, don't
force it into your progs...
--
regards, Zodiaq
|