How xor works. VBA Logical Operators. Scheme for identifying the edge and cutoff of a pulse

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

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

Logical operations, bitwise logical operations, and shift operations can only be used on operands of 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 “NO”.

The result of logical operations is the value false or true . In C++, false is considered to be 0 and true is considered to be 1.

From this we can conclude that false< true . Например:

// logical operations bool res; res = false< true ; // res = true
3. Truth table of logical operations

Truth table of logical operations && (logical “AND”), || (logical "OR"), ! (logical “NO”) has the following form:

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

4. Examples of using logical operations in C++

Example 1. Logical operation combined with a Boolean 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 "NO") a = 0; res = !a; // res = True a = 15; res = !a; // res = False

Example 2. Logical operation in conditional expressions. Here is a code fragment in which a 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. What bitwise logical operations are used in C/C++?

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

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

Operations & , ^ , | are binary. This means that they require two operands. The bits of any operand are compared to each other as follows: rule: The bit at position 0 of the first operand is compared with the bit at position 0 of the second operand. The bit at position 1 of the first operand is then compared with the bit at position 1 of the second operand. This compares all the bits of the integer operands.

6. Truth table 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 the 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 number occupies 1 byte or 8 bits in memory. Below is an example of how the calculation occurs for each bitwise operation

As can be seen from the example, a specified operation is performed on each bit.

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

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

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

The draw bits are lost and the zero bits are "in". Shifting operands to the left by 1, 2, 3 or more digits is the fastest way to multiply by 2, 4, 8, ... Shifting operands to the right by 1, 2, 3 or more digits is the fastest way to divide by 2, 4, 8, ...

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

This also applies to cases when you need to quickly divide an integer operand by 2, 4, 8, etc. In these cases, it is recommended to use shift right.

9. Examples of using shift operations in a program
// Shift operations int a; int b; int c; a = 15; b = -5; // shift left - multiply c = a<< 1; // c = a * 2^1 = 30 c = b << 2; // c = b * 2^2 = -20 // right shift - 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?

Logical operations compare the entire value of two operands. 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 a non-zero (any other) value corresponds to the value true .

the function they perform is somewhat more complex than in the case of an AND element or an OR element. All inputs of the XOR gates are equal, but no input can block the other inputs by setting the output to one or zero. Table 4.1. Truth table two-input exclusive OR elements
Input 1 Entrance 2 Exit
0 0 0
0 1 1
1 0 1
1 1 0


Rice. 4.1.

The Exclusive OR function means the following: a one appears at the output when only one input has a one. If there are two or more ones at the inputs, or if all inputs are zeros, then the output will be zero. Truth table two-input element Exclusive OR is given in table. 4.1. The designations adopted in domestic and foreign schemes are shown in Fig. 4.1. The inscription on the domestic designation of the element Exclusive OR "=1" just means that the situation is highlighted when there is one and only one unit at the inputs.

There are few XOR elements in standard series. Domestic series offer LP5 microcircuits (four two-input elements with a 2C output), LL3 and LP12, which differ from LP5 in the OK output. Too specific a function is implemented by these elements.

From a mathematical point of view, the XOR element performs the operation of the so-called modulo-2 summation. Therefore, these elements are also called modulo-two adders. As noted in the previous lecture, summation modulo 2 is indicated by a plus sign enclosed in a circle.

The main application of the elements is Exclusive OR, which follows directly from truth tables, consists of comparing two input signals. In the case when two ones or two zeros arrive at the inputs (the signals coincide), a zero is formed at the output (see Table 4.1). Typically, in this application, a constant level is applied to one input of the element, with which a time-varying signal arriving at the other input is compared. But much more often, special microcircuits are used to compare signals and codes code comparators, which will be discussed in the next lecture.

As a modulo 2 adder, the XOR element is also used in parallel and serial modulo 2 dividers used to calculate cyclic checksums. But these schemes will be discussed in detail in lectures 14,15.

An important application of XOR elements is a controlled inverter (Fig. 4.2). In this case, one of the element inputs is used as a control one, and an information signal is received at the other element input. If the control input is one, then the input signal is inverted, but if it is zero, it is not inverted. More often control signal is set at a constant level, determining the operating mode of the element, and the information signal is pulsed. That is, the XOR element may or may not change the polarity of the input signal or edge, depending on control signal.


Rice. 4.2.

In the case where there are two signals of the same polarity (positive or negative), and their simultaneous arrival is excluded, the Exclusive OR element can be used to mix these signals (Fig. 4.3). For any polarity of input signals, the output signals of the element will be positive. For positive input signals, the XOR gate will act as a 2OR gate, and for negative inputs, it will replace the 2AND-NOT gate. Such replacements can be useful in cases where some Exclusive OR elements remain unused in the circuit. True, it must be taken into account that propagation delay The signal in the XOR element is usually slightly larger (about 1.5 times) than the delay in the simplest AND, NAND, OR, NOR elements.

In practice, the most commonly used two-input elements are “exclusive OR”. In Fig. Figure 1 shows a conventional graphic designation of an element without inversion and its state table. Simply put, the essence of this element boils down to the following: the output signal appears only when the logical levels at the inputs are not the same.

Scheme for identifying the edge and cutoff of a pulse

In this circuit, three XOR gates are used to delay the pulses. DD1.4 - summing. Output pulses have stable leading edges and falling edges. The duration of each output pulse is equal to triple the switching delay time of each of the three elements. The time interval between the edges of the output pulses is equal to the duration of the input pulse. This device also doubles the frequency of the input signal.

There is another interesting property called “Exclusive OR”. If a constant “0” is applied to one of the inputs, then the signal at the output of the element will repeat the input signal, and if the constant “0” is changed to a constant “1”, then the output signal will already be an inversion of the input signal.

Sometimes it becomes necessary to obtain an "exclusive OR" gate from individual standard logical gates. An example is the “exclusive OR” element circuit implemented on four 2-AND-NOT elements. Figure 3 shows an XOR circuit in its four states. This shows all the possible logic levels at each of the 2-NAND logic gates used.

Such elements are included in the diagram. In this circuit, the “Exclusive OR” element is made of four 2-AND-NOT elements included in one package of the K561LA7 microcircuit.

Discrete signal generator with difference frequency

The driver circuit is shown in Figure 4. Here, the exclusive OR logic element is also implemented on four 2-AND-NOT elements.

Rectangular pulses fall at inputs 1 and 2 of the shaper (see graphs 1 and 2), which differ in repetition frequency. A node based on logical elements DD1.1-DDI.4 multiplies these signals. The output pulse signal (graph 3) from element DD1.4 is fed to the integrating circuit R3, C1, which converts it into a triangular signal (graph 4) with a frequency equal to the difference in the frequencies of the input signals, and op-amp DA1 converts the received signal into a square wave (see. schedule 5). Resistor R1 regulates the duration of the positive and negative half-waves of the output signal. A very interesting scheme. The radio designer has something to think about. For example, the signal shown in the third graph is a sine wave PWM signal.
Of course, the range of use of “exclusive OR” elements is much wider. I have presented here, in my opinion, more interesting for radio amateurs.

In this article we will talk about some bit operations. Let's look at the main ones: XOR (exclusive OR), AND, NOT and OR.

As is known, the minimum unit of measurement of information is bit, which stores one of 2 values: 0 ( False, false) or 1 ( True, true). Thus, a bit cell can only be in one of two possible states at a time.

To manipulate bits, certain operations are used - logical or boolean. They can be applied to any bit, regardless of whether its value is zero or one. Well, let's look at examples of the use of three basic logical operations.

Logical operation AND (and)

AND denoted by &.

The AND operator is performed on 2 bits, let's take a and b for example. The result of the AND operation is 1 if a and b are equal to 1. Otherwise, the result is 0. For example, using AND you can find out whether a number is even or not.

Look at the truth table of the AND operation:

Logical operation OR (OR)

Indicated by | .

Operator OR also done with 2 bits (a and b). The result is 0 if a and b are 0, otherwise it is 1. Look at the truth table.

Logical operation XOR (exclusive OR)

The XOR operator is denoted by ^.

XOR executed with 2 bits (a and b). The result of the XOR operation ( exclusive OR) is 1 when one of the bits b or a is 1. Otherwise, the result of applying the XOR operator is 0.

The logical operation truth table for XOR (exclusive OR) looks like this:

Using XOR (exclusive OR), you can change the values ​​of 2 variables of the same data type without using a temporary variable. You can also encrypt text using XOR, for example:

String msg = "This is a message"; char message = msg.toCharArray(); String key = ".*)"; String encryptedString = new String(); for(int i = 0; i< message.length; i++){ encryptedString += message[i]^key.toCharArray(); }

I agree that XOR is far from the most reliable encryption method, but this does not mean that it cannot be made part of any encryption algorithm.

Logical operation NOT (NOT)

This is a bitwise negation, so is performed with one bit and is denoted by ~ .

The result depends on the state of the bit. If it is in the zero state, then the result of the operation is one and vice versa. Everything is extremely simple.

These 4 logical operations should be remembered first, because with their help you can get almost any possible result. There are also operations such as<< (побитовый сдвиг влево) и >> (bitwise right shift).

The XOR instruction in Assembly language performs an exclusive OR operation between all the bits of two operands. The result of the XOR operation is written to the first operand. Syntax:

XOR RECEIVER, SOURCE

The XOR instruction always resets CF and OF, and also (depending on the result) changes the SF, ZF, and PF flags. The value of the AF flag can be anything - it does not depend on the result of the operation.

The RECEIVER may be one of the following:

  • Memory area (MEM)

The SOURCE may be one of the following:

  • Memory area (MEM)
  • General purpose register (REG)
  • Immediate value - constant (IMM)

Subject to the limitations described above, the RECEIVER-SOURCE combinations can be as follows:

REG, MEM MEM, REG REG, REG MEM, IMM REG, IMM

Exclusive OR operation

When performing an exclusive OR operation, the result value will be 1 if the bits being compared are different (not equal). If the compared bits have the same value, then the result will be 0.

That is why this operation is called exclusive. It excludes identical bits from comparison, and performs the operation on unequal bits.

But, since any pair of unequal bits is 0 and 1, the logical OR operation will result in 1.

Exclusive OR truth table

The XOR truth table is given below:

0 XOR 0 = 0 0 XOR 1 = 1 1 XOR 0 = 1 1 XOR 1 = 0

Features of the XOR operation

The XOR operation has the property of reversibility. If it is executed twice with the same operand, the result value is inverted. That is, if you perform this operation twice between the bits X And Y, then in the final result we will get the original bit value X.

0 XOR 0 = 0 XOR 0 = 0 0 XOR 1 = 1 XOR 1 = 0 1 XOR 0 = 1 XOR 0 = 1 1 XOR 1 = 0 XOR 1 = 1

This property can be used, for example, for simple data encryption (more on this some other time).

Checking the parity flag after XOR operation

The XOR command works with 8-, 16-, and 32-bit operations.

Sometimes there is a need after performing an operation to check the parity flag PF in order to find out how many one bits (even or odd) are contained in low byte result (this is necessary not only when performing an XOR operation, but also when performing other arithmetic and logical operations).

If the parity flag is set, the result is an even number of one bits. Otherwise the flag will be reset.

You can also simply check any number for parity without changing the value of the result. To do this, you need to execute the XOR command with a zero value. That is, the RECEIVER must contain the number being tested, and the SOURCE must contain zero. And then you need to check the parity flag. Example:

AL, 10110101b ;Place in AL a number with an odd;number of one bits (5) XOR AL, 0 ;In this case, the parity flag PF is not;set (PO) MOV AL, 10110111b ;Place in AL a number with an even;number of one bits (6 ) XOR AL, 0 ;In this case, the parity flag PF ;will be set (PE)

In debuggers, the abbreviation PE (Parity Even) is usually used to denote an even number of ones in the resulting result, and PO (Parity Odd) for an odd number.

Parity in 16-bit words

As already mentioned, the parity flag is set depending on the number of ones contained in the low byte of the result. To check the parity of a 16-bit operand, you need to XOR the high and low byte of the number:

MOV AX, 64C1h ;0110 0100 1100 0001 - 6 one bits XOR AH, AL ;Parity flag will be set

In this simple way, the 16-bit operand is divided into two bytes (2 groups of 8 bits), and when executing the XOR command, the one bits located in the corresponding bits of the two 8-bit operands will not be taken into account. Because the corresponding bit of the result is zero.

The XOR instruction removes any overlapping one bits of two 8-bit operands from the result and adds disjoint one bits to the result. That is, the parity of the 8-bit number we receive will be the same as the parity of the original 16-bit number.

0110 0100 1100 0001 - original 16-bit number 0 XOR 1 = 1 1 XOR 1 = 0 1 XOR 0 = 1 0 XOR 0 = 0 0 XOR 0 = 0 1 XOR 0 = 1 0 XOR 0 = 0 0 XOR 1 = 1

The result is 4 units, that is, the PF flag will be set

Parity in 32-bit double words

Well, what if you need to determine the parity of a 32-bit number?

Then the number is divided into four bytes, and an exclusive OR operation is performed on these bytes one by one.

For example, we split a 32-bit number B by four bytes B0, B1, B2, B3, Where B0- this is the low byte.

Then to determine the parity of number B, we will need to use the following formula:

B0 XOR B1 XOR B2 XOR B3

But in assembler such a notation is not allowed. So you'll have to think a little.

And finally, about the origin of mnemonics XOR. In English there is a word e X reception - exception. The abbreviation for this word is the letter X(that's how it happened). You've probably seen this in advertising or in the names of products whose manufacturers claim (or think they claim) exclusivity. For example, Lada XRAY, Sony XPeria, etc. So XOR is an acronym made up of two words - e X reception OR- exclusive OR.

Continuing the topic:
Devices

Lviv National University named after. I. Franko (LNU) - additional information about the higher educational institution General information Lviv National University named after Ivan...