I’m looking for a way to figure out the passed arguments (as well as their order) in a function out of a possibly precalculated integer by bitwise operators. I’m already aware that I should be using numbers like 2^n for the constants that can be passed (due to bitwise calculations) but I don’t have an idea how to decompose that.
So to sum up:
- How can I figure out that 1 and 4 has been passed to the following function
- Can the order of these be obtained somehow, perhaps by some specific bitwise operator?
Here’s an example displaying what I mean.
$inst = new SomeClass;
$inst->some_func( SomeClass::RULE1 | SomeClass::RULE3 );
class SomeClass {
const RULE1 = 1;
const RULE2 = 2;
const RULE3 = 4;
public function some_func($arg) {
// what are the RULE's in the $arg here?
}
Please note that I’m trying to figure out how to work with bitwise operators as function arguments. So I’m not looking for other solutions like passing an array to the function or using func_get_args().
Well lets take a look (syntax is for PHP 5.4):
So when you pass in
$inst->some_func( SomeClass::RULE1 | SomeClass::RULE3 );;The arg will be
0b0000 0101.So now to actually use this, we would do:
Hopefully that helps!
That wouldn’t be possible to do, because
RULE1 | RULE3andRULE3 | RULE1both produce the exact same binary sequence.