# bitwise vs logical operators

• Posts: 131

3+ Months Ago

I'm referring to php mainly, but I know this applies in other languages as well:

Is there a difference between using logical operators and bitwise operators? It seems like using bitwise would always be the most efficient, since it is processed much quicker.

example in quest:

PHP Code: [ Select ]
a = 5;
b = 10;

if (a>4 && b<20){
execute code;
} //using two ampersands is logical

if (a>4 & b<20){
execute code;
}   //If I am to understand correctly, simply using 1 ampersand is bitwise

//essentially the bitwise condition is asking:

if (1 & 1){execute code;};

//Where both conditions must be true to execute;

Also:

if (1 ^ 0){} //This would also execute;

What would be the difference in using:

if (1 xor 0){};

Realworld application:

if (a>4 ^ b<20){};

OR:

if (a>4 xor b<20){};

1. a = 5;
2. b = 10;
3.
4. if (a>4 && b<20){
5.    execute code;
6.    } //using two ampersands is logical
7.
8. if (a>4 & b<20){
9.    execute code;
10.    }   //If I am to understand correctly, simply using 1 ampersand is bitwise
11.
12. //essentially the bitwise condition is asking:
13.
14. if (1 & 1){execute code;};
15.
16. //Where both conditions must be true to execute;
17.
18. Also:
19.
20. if (1 ^ 0){} //This would also execute;
21.
22. What would be the difference in using:
23.
24. if (1 xor 0){};
25.
26. Realworld application:
27.
28. if (a>4 ^ b<20){};
29.
30. OR:
31.
32. if (a>4 xor b<20){};
33.
34.

If the statement is false, then the computer reads it as a 0, true is 1. (Everything here is written quizzically, not assertions, so correct me if I'm wrong anywhere).

Don't both of these test the exact same logic? If so, when would it ever be better to use logical operators over bitwise operators?

The only things I've seen are situations where bitwise can be more useful than logical operators. So, my main question is there any situation where logical operators are better. Or should one always use bitwise.

Please let me know if my understanding of this is flawed anywhere.

EDIT:

With some testing, I found a situation where logical operators will work and bitwise operators wouldn't:

In binary a float number like 0.5 is read as a 0. So testing it with bitwise will return false, even though it technically isn't. Using logical operators force it to true, before it is processed in binary.
PHP Code: [ Select ]
if(0.5 | 0){execute}; //does not execute; reads 0.5 is 0 in binary

if(0.5 || 0){execute}; //Logical turns the 0.5 into a 1 (true), so this is executed.

1. if(0.5 | 0){execute}; //does not execute; reads 0.5 is 0 in binary
2.
3. if(0.5 || 0){execute}; //Logical turns the 0.5 into a 1 (true), so this is executed.
4.
5.

It also seems like the bitwise (~) not operator has little to no comparative value.
• Mastermind
• Posts: 2391
• Loc: Edinburgh, Scotland

3+ Months Ago

There is a definite difference - bitwise operations don't just return true or false, they perform an operation on the two operands and return the value. For instance, (9 & 8 ) returns 8, i.e. the binary operation 9 & 8 can be illustrated as:
Code: [ Select ]
8 4 2 1
----------
9: 1 0 0 1
8: 1 0 0 0
----------
1 0 0 0
1.   8 4 2 1
2. ----------
3. 9: 1 0 0 1
4. 8: 1 0 0 0
5. ----------
6.   1 0 0 0

As a result, when you include a bitwise operation in an if statement, what you are checking for is a non-zero result, not a logical true or false. PHP interprets non-zero values as being true. As such, you may experience slightly differing results if you start testing using the triple equals syntax, i.e.:
PHP Code: [ Select ]
// Prints out "Foobar"
if(9 & <!-- s8) --><img src=\"{SMILIES_PATH}/icon_cool.gif\" alt=\"8)\" title=\"Cool\"><!-- s8) --> {
echo "Foobar";
}

\$result = (9 & <!-- s8) --><img src=\"{SMILIES_PATH}/icon_cool.gif\" alt=\"8)\" title=\"Cool\"><!-- s8) -->;
// Does not print out!
if(\$result === true) {
echo "Barfoo";
}

1. // Prints out "Foobar"
2. if(9 & <!-- s8) --><img src=\"{SMILIES_PATH}/icon_cool.gif\" alt=\"8)\" title=\"Cool\"><!-- s8) --> {
3.    echo "Foobar";
4. }
5.
6. \$result = (9 & <!-- s8) --><img src=\"{SMILIES_PATH}/icon_cool.gif\" alt=\"8)\" title=\"Cool\"><!-- s8) -->;
7. // Does not print out!
8. if(\$result === true) {
9.    echo "Barfoo";
10. }
11.

The second echo statement doesn't execute, because the result of the bitwise 'AND' returns a non-zero integer, not a boolean. If the 'if' statement had used double equals ('=='), the echo statement would have executed as the non-zero integer would be interpreted as true. The treble equals checks both value and type.

## Post Information

• Total Posts in this topic: 2 posts
• Users browsing this forum: No registered users and 34 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum