I read the following Stack Overflow questions, and I understand the differences between bitwise and logical.
However, none of them explains when I should use bitwise or logical.
When should I use bitwise operators rather than logical ones and vice versa?
In which situation do I need to compare bit by bit?
I am not asking about the differences, but I am asking the situation when you need to use bitwise operators.
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:
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.
We are done. Let's check the $delete permission for all 3 roles:
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.
Well, asuming there is
$x = (false && some_function());
, here the value of$x
will be set without calling thesome_function()
because the first value was FALSE.But what if you needed to call that function anyway? Use
$x = (false & some_function());
.In other words, the
&
uses MORE processing than the &&, just because && does not run through all the values to check them. If it found one value as false, it would return it, not looking at other values.And in logical operations, use the
&&
operator as it's used to return the logical operation value, where&
is used to set a value and in anif
statement it will always return true.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:
Logical operators compare two (or more) conditions/expressions and return true or false. You use them most commonly in conditional statements, like
if
andwhile
. For example:Bitwise is useful for things in PHP just like anything else.
How about a value that can have multiple states turned on at the same time?
Output:
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 specifically used in case of binary value representation.
Logical operators are most preferred for comparison and are a bit faster than bitwise operator in case of
AND
andXOR
operations.if
func1()
returns false it won't bother callingfunc2()
Will call both functions regardless of their returned values.