I read the following Stack Overflow questions, and I understand the differences between bitwise and logical.
Difference between & and && in
Forget what is already in your head.
OK, now say you have some different roles: admin, user, and guest.
and some different permissions: read, write and delete
Let's create some bitmasks for permissions and roles. A bitmask is a sequence of bits that can be used to manipulate or read some kind of flags. As shown below:
// flags bitmasks
$read = 1; // 0001
$write = 2; // 0010
$delete = 4; // 0100
$admin = $read | $write | $delete; // 0001 | 0010 | 0100 => 0111
$user = $read | $write; // 0001 | 0010 => 0011
$guest = $read; // 0001 => 0001
Notice 1, 2, 4. This must be raised as double. Otherwise, it might give you some awkward results.
Forget about the things commented. Those are just sequence of bits (or bitmasks) for individual permissions and roles.
Now let's create a handy function which may be used to check a specific permission for a specific role.
function isAllowed($role, $permissison) {
return $role & $permissison ? true : false;
}
We are done. Let's check the $delete permission for all 3 roles:
var_dump(isAllowed($admin, $delete)); // bool(true)
var_dump(isAllowed($user, $delete)); // bool(false)
var_dump(isAllowed($guest, $delete)); // bool(false)
So why is bitwise operation? In a word, bitwise operation is more faster, concise and maintainable. Otherwise, using bitwise operation is always efficient for complex applications.
Bitwise |
and &
and logical ||
and &&
are totally different.
Bitwise operators perform operations on the bits of two numbers and return the result. That means it's not a yes or no thing. If they're being used in conditional statements, they're often used as part of logical comparisons. For example:
if ($x & 2 == 2) {
// The 2^1 bit is set in the number $x
}
Logical operators compare two (or more) conditions/expressions and return true or false. You use them most commonly in conditional statements, like if
and while
. For example:
if ($either_this || $or_this) {
// Either expression was true
}
Bitwise operators are specifically used in case of binary value representation.
echo '0110011010' & '0101001001';
//0100001000
Logical operators are most preferred for comparison and are a bit faster than bitwise operator in case of AND
and XOR
operations.
if(func1() && func2())
if func1()
returns false it won't bother calling func2()
if(func1() & func2())
Will call both functions regardless of their returned values.
They are two very different operators.
When you want two conditions to simultaneously be true, you use the logical &&
. For example, I want to print out "happy birthday" only if the person's birthday is today && they have money in the account. This is used whenever we want to perform composition of two or more conditions, mostly in if statements and loop conditions (although not exclusively).
When you want to perform bitwise operations (which in day-to-day PHP programming is much more rare), you use the bitwise &
. This is far more rare, and you might be performing bitwise masking (I doubt it though), so you might only want a result that both integers represent, in which case you can say newAttribute = attribute1 & attribute2;
.
If you want to know more how it works check out this site:
http://www.bitwiseoperatorcalculator.com/
Coming from PHP, I have never used it before but just reading for fun this site gives simple explanations!
Bitwise operators are useful when manipulating the bits of a number. Look here. Otherwise you should use the logical operators. Also the logical operators are short-circuited. For example, if you have a && b
and a
is false
, b
is not evaluated.
Disclaimer: I am coming from a Java background, but I guess in PHP it is the same.