AND and OR operators - what is the difference between logical and bitwise varieties?
Many 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) format
00000000000000000000000001000011
is 67 in binary (internal) format
Bitwise "or" gives
00000000000000000000000001010111
which is 87 in binary (internal) format
Logical 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.
(written 2010-12-24)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q110 - Object Orientation and General technical topics - Programming Algorithms [202] Searching for numbers - (2005-02-04)
[227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[642] How similar are two words - (2006-03-11)
[1157] Speed Networking - a great evening and how we arranged it - (2007-04-21)
[1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1840] Validating Credit Card Numbers - (2008-10-14)
[1949] Nuclear Physics comes to our web site - (2008-12-17)
[2189] Matching disparate referencing systems (MediaWiki, PHP, also Tcl) - (2009-05-19)
[2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
[2509] A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21)
[2586] And and Or illustrated by locks - (2010-01-17)
[2617] Comparing floating point numbers - a word of caution and a solution - (2010-02-01)
[2894] Sorting people by their names - (2010-07-29)
[2951] Lots of way of converting 3 letter month abbreviations to numbers - (2010-09-10)
[2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
[3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3093] How many toilet rolls - hotel inventory and useage - (2010-12-18)
[3451] Why would you want to use a Perl hash? - (2011-09-20)
[3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
[3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
[4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
[4401] Selecting RECENT and POPULAR news and trends for your web site users - (2015-01-19)
[4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
[4410] A good example of recursion - a real use in Python - (2015-02-01)
[4652] Testing new algorithms in PHP - (2016-02-20)
[4656] Identifying the first and last records in a sequence - (2016-02-26)
[4707] Some gems from an introduction to Python - (2016-10-29)
P202 - Perl Fundamentals [184] MTBF of coffee machines - (2005-01-20)
[748] Getting rid of variables after you have finished with them - (2006-06-06)
[1312] Some one line Perl tips and techniques - (2007-08-21)
[1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
[1726] Hot Courses - Perl - (2008-07-28)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1946] Variable Types in Perl - (2008-12-15)
[2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3329] Perl from basics - (2011-06-20)
[3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
[3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
[3574] Perl functions such as chop change their input parameters - (2012-01-10)
[3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[4324] Learning to program - variables and constants - (2014-11-22)
Some other Articles
BucketsAdventure with references to lists and lists of referencesCatering in Syracuse, the Saigon Cafe, stolen images and ChristmasThank you - and Happy ChristmasAND and OR operators - what is the difference between logical and bitwise varieties?The week before ChristmasLooking ahead and behind in Regular Expressions - double matchingPerl - database access - DBD, DBI and DBIx modulesLearning Object Orientation in Perl through bananas and perhaps MooseMaking Perl class definitions more conventional and shorter