Training, Open Source Programming Languages

This is page http://www.wellho.net/mouth/3102_AND ... ties-.html

Our email: info@wellho.net • Phone: 01144 1225 708225

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
P202 - Perl Fundamentals
  [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
  [3574] Perl functions such as chop change their input parameters - (2012-01-10)
  [3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
  [3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
  [3329] Perl from basics - (2011-06-20)
  [3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
  [3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
  [2876] Different perl examples - some corners I rarely explore - (2010-07-18)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
  [1946] Variable Types in Perl - (2008-12-15)
  [1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
  [1726] Hot Courses - Perl - (2008-07-28)
  [1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
  [1312] Some one line Perl tips and techniques - (2007-08-21)
  [748] Getting rid of variables after you have finished with them - (2006-06-06)
  [184] MTBF of coffee machines - (2005-01-20)

Q110 - Object Orientation and General technical topics - Programming Algorithms
  [3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
  [3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
  [3451] Why would you want to use a Perl hash? - (2011-09-20)
  [3093] How many toilet rolls - hotel inventory and useage - (2010-12-18)
  [3072] Finding elements common to many lists / arrays - (2010-11-26)
  [3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
  [2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
  [2951] Lots of way of converting 3 letter month abbreviations to numbers - (2010-09-10)
  [2894] Sorting people by their names - (2010-07-29)
  [2617] Comparing floating point numbers - a word of caution and a solution - (2010-02-01)
  [2586] And and Or illustrated by locks - (2010-01-17)
  [2509] A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21)
  [2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
  [2189] Matching disparate referencing systems (MediaWiki, PHP, also Tcl) - (2009-05-19)
  [1949] Nuclear Physics comes to our web site - (2008-12-17)
  [1840] Validating Credit Card Numbers - (2008-10-14)
  [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
  [1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
  [1157] Speed Networking - a great evening and how we arranged it - (2007-04-21)
  [642] How similar are two words - (2006-03-11)
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
  [202] Searching for numbers - (2005-02-04)


Back to
The week before Christmas
Previous and next
or
Horse's mouth home
Forward to
Thank you - and Happy Christmas
Some other Articles
Buckets
Adventure with references to lists and lists of references
Catering in Syracuse, the Saigon Cafe, stolen images and Christmas
Thank you - and Happy Christmas
AND and OR operators - what is the difference between logical and bitwise varieties?
The week before Christmas
Looking ahead and behind in Regular Expressions - double matching
Perl - database access - DBD, DBI and DBIx modules
Learning Object Orientation in Perl through bananas and perhaps Moose
Making Perl class definitions more conventional and shorter
4316 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3102_AND ... ties-.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat