C++. Logical operations. Bitwise logical operations. Shift operations. The XOR operation

C++. Logical operations. Bitwise logical operations. Shift operations. The XOR operation


Content

  1. For what types can be used the logical operations, bitwise logical operations and shift operations ?
  2. What logical operations are used in C/C ++?
  3. The truth table of logic operations
  4. Examples of using logical operations in C++
  5. Which bitwise logical operations are used in C/C++?
  6. Table of truth of bitwise logical operations
  7. An example of working with logical bitwise operations
  8. What shift operations are used in C/C ++?
  9. Examples of using shift operations in the program
  10. What is the difference between logical operations and bitwise logical operations?
  11. How to implement a logical XOR operation in C++?

1. For what types can be used the logic operations, bitwise logical operations and shift operations ?

Logical operations, bitwise logical operations, and shift operations can only be used for integer types.

 

2. What logical operations are used in C/C ++?

The following logical operations are used in the C/C++ programming language:

  • && – logical “AND“;
  • || – logical “OR“;
  • ! – logical “NOT“.

The result of the logical operations is false or true. In C++, it is assumed that false is set to 0, and true is set to 1. Hence, we can conclude that false < true. For example:

// logical operations
bool res;

res = false < true; // res = true

 

3. The truth table of logic operations

The truth table of logical operations && (logical “AND”), || (logical “OR”), ! (Logical “NOT”) is as follows:

In C/C ++, it is assumed that false is 0, and true is not 0 (any non-zero integer value).

 

4. Examples of using logical operations in C++

Example 1. Logical operation in combination with a logical expression

// logical operations
bool res;
int a, b;

// operation && (AND)
a = 8;
b = 5;
res = a && b; // res = True

a = 0;
res = a && b; // res = False

// operation || (OR)
a = 0;
b = 0;
res = a || b; // res = False

b = 7;
res = a || b; // res = True

// operation ! (logical "NOT")
a = 0;
res = !a; // res = True

a = 15;
res = !a; // res = False

Example 2. Logical operation in conditional expressions. A code snippet is provided in which the logical operation is used in the ‘if’ statement.

// logical operations in conditional expressions
int a, b;
bool res;

a = 0;
b = 3;
res = false;
if (a && b)
    res = true; // res = false

a = 0;
b = 7;
if (a || b)
    res = true; // res = true

 

5. Which bitwise logical operations are used in C/C++?

The C/C ++ language supports the following bitwise logical operations:

  • & – bitwise logical AND;
  • ^ – bitwise addition modulo 2 (XOR);
  • | – bitwise logical OR;
  • ~ – bitwise inversion NOT.

The operations &, ^, | are binary. This means that they require two operands. The bits of any operand are compared with each other according to the following rule:

  • the bit at position 0 of the first operand is compared with the bit at position 0 of the second operand. Then, the bit at position 1 of the first operand is compared with the bit at position 1 of the second operand. This compares all the bits of the integer operands.

 

6. Table of truth of bitwise logical operations

Each bit of the result is determined based on two operands, which are bits, as shown in the table.

Inversion requires a single operand to the right of the ‘~’ sign. The result is obtained by bitwise inversion of all bits of the operand.

 

7. An example of working with logical bitwise operations

Let two numbers 17 and 45 of type unsigned short int be given. Each of the numbers occupies 1 byte or 8 bits in memory. Below is an example of how a calculation is performed for each bitwise operation

 

As you can see from the example, the given operation is performed on each bit.

 

8. What shift operations are used in C/C ++?

The C/C++ language includes two bitwise shift operations:

  • << – shift to the left the value of the operand by a specified number of bits. The operand is placed to the left of the operation sign (<<). The number of shifted bits is indicated to the right of the sign of the operation;
  • >>  – shift to the right the value of the operand by a specified number of bits. The operand is placed to the left of the operation sign (>>). The number of shifted bits is indicated to the right of the sign of the operation;

Retractable bits are lost, and “zero” bits are entered. The shift of the operands to the left by 1, 2, 3 or more digits is the fastest way of multiplying by 2, 4, 8, … The shift of the operands to the right by 1, 2, 3 or more digits is the fastest way of dividing by 2, 4, 8, …

If the program requires that the operation of multiplying integer operands by 2, 4, 8 etc. occur as quickly as possible, then it is advisable to use the shift operation to the left.

This also applies to cases where it is necessary to divide the integer operand as fast as possible into 2, 4, 8, etc.

 

9. Examples of using shift operations in the program
// the shift operations
int a;
int b;
int c;

a = 15;
b = -5;

// shift left - multiplication
c = a << 1; // c = a * 2^1 = 30
c = b << 2; // c = b * 2^2 = -20

// shift right - division
c = a >> 3; // c = a / 2^3 = 1
c = b >> 1; // c = b / 2^1 = -3

 

10. What is the difference between logical operations and bitwise logical operations?

In logical operations, the value of the two operands is compared in entirety. Each of the operands can be true or false. The C/C++ language allows comparison of operands that are integers. In this case, the integer value 0 corresponds to the value false, and the nonzero value (any other) corresponds to the value true.

Bitwise logical operations work strictly with the bits of any operand. A bit can have 2 values: 0 or 1. Therefore, the corresponding bits of each operand are subject to calculation and not the value of the operand as a whole.

 

11. How to implement a logical XOR operation in C++?

As you know, the C++ language does not contain the built-in logical operation XOR.

Here is the code fragment that implements the XOR operation by using the && (AND), || (OR), ! (NOT) operations.

// the implementation of the XOR operation through && (AND), || (OR),! (NOT)
bool x, y;
bool res;

...

res = (x || y) && !(x && y);

...

In the above code:

  • x, y – variables for which the operation XOR is calculated;
  • res – a variable that is the result of a calculation.

 


Related topics