Learn programming with C++ : Logical Operations

c-logo

Previous Post: Learn programming with C++ : Mathematical Operations

The most common statement in C++ is the expression. Most expressions involve the arithmetic operators, such as addition (+), subtraction (–) and multiplication (*). But there is a whole other class of operators, known as the logical operators.

Logical operators fall into two types. The AND and OR operators are what we will call simple logical operators. The second type of logical operator is the bitwise operator. People don’t use the bitwise operator in their daily life at all, it’s unique to the computer world.

The simple logical operators evaluate to true or false. See below to know what are the simple logical operators:

Operator Work
>, < Greater than, less than; true if the left-hand argument is greater than or less than the right-hand argument
>=, <= Greater than or equal to, less than or equal to; true if either > or == is true, or either < or == is true
&& AND; true if both the left- and right-hand arguments are true
|| OR; true if either the left- or right-hand argument is true
! NOT; true if its argument is false; otherwise, false
== Equality; true if the left-hand argument has the same value as the right
!= Inequality; opposite of equality

The equality operator is used to compare two numbers. For example, the following is true if the value of n is 0, and is false otherwise:

Don’t confuse the equality operator (==) with the assignment operator (=). Not only is this a common mistake, but it’s a mistake that the C++ compiler generally cannot catch — that makes it more than twice as bad. The following statement does not initialize n to 0, it compares the current value of n with 0 and then does nothing with the results of that comparison.

The greater-than ( > ) and less-than ( < ) operators are similarly common in everyday life. The following logical comparison is true:

The greater-than-or-equal-to operator (>=) and the less-than-or-equal-to operator (<=) are similar to the less-than and greater-than operators, with one major exception. They include equality; the other operators don’t. The && (AND) and || (OR) work in combination with the other logic operators to build more complex logical expressions, like this:

 

Storing logical values

The result of a logical operation can be assigned to a variable of type bool.

 

C++ hasn’t always had a bool type variable. C++ used int variables to store logical values. A value of 0 was considered false and all other values true. By the same token, a logical operator generated a 0 for false and a 1 for true. (Thus, 10 < 5 returned 0 while 10 > 5 returned 1.) C++ retains a high degree of compatibility between bool and int to support the older programs.

The logical AND && and logical OR || operators perform what is called shortcircuit evaluation. Consider the following:

If condition1 is not true, the overall result is not true, no matter what the value of condition2. (For example, condition2 could be true or false without changing the result.) The same situation occurs in the following:

If condition1 is true, the result is true, no matter what the value of condition2 is. To save time, C++ doesn’t evaluate condition2 if it doesn’t need to. For example, in the expression condition1 && condition2, C++ doesn’t evaluate condition2 if condition1 is false. Likewise, in the expression condition1 || condition2, C++ doesn’t evaluate condition2 if condition1 is true. This is known as short-circuit evaluation.

All C++ numbers can be expressed in binary form. Binary numbers use only
the digits 1 and 0 to represent a value. Table 4-2 defines the set of operations
that work on numbers one bit at a time, hence the term bitwise operators.

 

All C++ numbers can be expressed in binary form. Binary numbers use only the digits 1 and 0 to represent a value. Table bellow defines the set of operations that work on numbers one bit at a time, hence the term bitwise operators.

Operator Function
~ NOT: toggle each bit from 1 to 0 and from 0 to 1
& AND each bit of the left-hand argument with that on the right
| OR each bit of the left-hand argument with that on the right
^ XOR (exclusive OR) each bit of the left-hand argument with that on
the right

Bit operations can be extremely fast. No performance penalty is paid for that 32-to-1 savings. Even though memory is cheap these days, it’s not unlimited. Sometimes, when you’re storing large amounts of data, this ability to pack a whole lot of properties into a single word is a big advantage.

 

Comments