Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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)


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
4759 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, 88, 89, 90, 91, 92, 93, 94, 95, 96 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).

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/mouth/3102_AND ... ties-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb