bitwise / binary questions
Posted: Wed Sep 26, 2007 6:42 pm
I am studying a PHP library right now. In the library, they define some simple flags like so...
Then they use these flags to denote whether certain parameters are required, optional, and if they should be only allowed once per component:
Why define these flags as hex? Why not just define them as decimal 1, 2 and 4? I don't understand the benefit here.
EDIT- and also, how would you then enforce this using these? I see later on they do this...
But I'm kind of lost here.
Code: Select all
define('FLAG_REQUIRED', 0x01); // binary 0001
define('FLAG_OPTIONAL', 0x02); // binary 0010
define('FLAG_ONCE', 0x04); // binary 0100Code: Select all
$this->valid_properties = array(
'SOMETHING' => FLAG_OPTIONAL | FLAG_ONCE, // binary 0110
'SOMETHING_ELSE' => FLAG_OPTIONAL | FLAG_ONCE, // binary 0110
'SOME_PROPERTY' => FLAG_REQUIRED | FLAG_ONCE, // binary 0101
'PROPALICIOUS' => FLAG_REQUIRED | FLAG_ONCE, // binary 0101
'I_AM_PROPERTASTIC' => FLAG_OPTIONAL // binary 0010
);EDIT- and also, how would you then enforce this using these? I see later on they do this...
Code: Select all
if(($propdata & FLAG_REQUIRED) && // some other condition) {
// snip
}