AND and OR operators - what is the difference between logical and bitwise varieties?
Archive - Originally posted on "The Horse's Mouth" - 2010-12-24 06:50:04 - Graham EllisMany modern programming languages have two operators for "or" and two for "and" - described as "bitwise" and "logical" operators. And you need to choose the right one in the right circumstances ... otherwise your code won't behave quite as you expect.
Bitwise OR takes the internal bit pattern of the two expressions / variables passed into it, and combines them bit by bit, setting a true (1) bit on the output if either of the incoming bits in the corresponding position is true. For example - illustrated with integers on a 32 bit system:
00000000000000000000000000010111
is 23 in binary (internal) format00000000000000000000000001000011
is 67 in binary (internal) formatBitwise "or" gives
00000000000000000000000001010111
which is 87 in binary (internal) formatLogical OR looks at each of the two incoming expressions as a whole, and if either of them is considered to be "true" in the language you're using, it returns a true result. But if both of the incoming expressions / variables are considered to be false, then a false result is returned.
99 times in 100, you'll want to use the logical OR rather than the bitwise OR in your code. "If the temperature is under 0 degrees OR if it is snowing" is a logical decision, for example - and most code requirements that combine conditions are comparisons like that. Uses of bitwise OR include setting up of masks (such as with file locking or selecting channels in Perl, and with regular expression modifiers in Python), and if you're doing low level computer graphics work, where a 32 bit byte may represent 32 pixels in a frame buffer, and you want to turn on extra bits as you draw a vector / fill an area on an image you're generating.
There is a similar differentiation bitwise AND and logical AND ... and it's with these that you can get badly burned if you use the wrong one. Consider 23 and 64 ...
• Logically, the result is TRUE
• Bitwise, there are NO bits set in both number, so the result is 0 which is usually FALSE (when misused in a logical context)
The really nasty snare here is that with most inputs, a bitwise and will return a true, lulling you into a false sense of security as you test your code.
In Perl, you have the following bitwise operators:
| bitwise OR
& bitwise AND
^ bitwise exclusive OR (XOR)
~ bitwise NOT (compliment)
<< bitwise left shift
>> bitwise right shift
and you have the following logical operators:
&& logical AND
|| logical or
and also logical AND
or also logical or
! logical not
and you'll find it's very similar in other languages such as C, C++ and PHP. There's an example of each of them in code, and sample output too, in our programs directory - [here].
With Python (which I might describe as a "post modern language", you only have a single operator for "or" and a single operator for "and" - they are | and &. The object type to the left of the operator determines if you're calling for a bitwise or logical operation.