Python Notes-XOR operation, OR operation, and operation

operate Features Application scenarios
XOR If the two operands are different, the result is 1; if they are the same, the result is 0. Encryption algorithms, checksum calculations, finding elements that appear an odd number of times in an array, etc.
or If one of the two operands is 1, the result is 1 Logical judgment, authority control, switch status judgment, etc.
and If both operands are 1, the result is 1 Mask operation, bit operation, logical judgment, etc.

illustrate:

  1. Exclusive OR operation (XOR): When the two operands are different, the result is 1; when the two operands are the same, the result is 0. The XOR operation has the characteristics of reflexivity (A XOR A = 0) and commutative law (A XOR B = B XOR A). The XOR operation is widely used in scenarios such as encryption algorithms, checksum calculations, and finding elements that appear an odd number of times in an array.

  2. Or operation (OR): When one of the two operands is 1, the result is 1; when both operands are 0, the result is 0. Or operations are often used in scenarios such as logical judgment, permission control, and switch status judgment.

  3. AND operation (AND): When both operands are 1, the result is 1; when one of the two operands is 0, the result is 0. AND operations are often used in mask operations, bit operations, logical judgments and other scenarios.

These logical operations are widely used in the fields of computer science and electronic engineering and can be used to handle tasks such as logical judgment, data encryption, data transmission verification, and bit operations. In programming and circuit design, these logic operations provide the basis for building complex logic and functionality.

XOR operation

XOR operation (XOR, Exclusive OR) is a binary operation that applies to each bit of two binary numbers. Its operation rules are as follows:

  1. If the two bits are the same (both 0 or both 1), the result is 0.
  2. If the two bits are different (one is 0 and one is 1), the result is 1.

The commonly used symbol for XOR operation is , usually expressed as " " in programming languages .

The XOR operation has some important properties:

  1. The result of XORing any number with 0 is still the number itself, a^0 = a.
  2. The result of any number XOR operation with itself is 0, that is, a ^ a = 0.
  3. The XOR operation satisfies the commutative law, that is, a ^ b = b ^ a.
  4. The XOR operation satisfies the associative law, that is, (a ^ b) ^ c = a ^ (b ^ c).

Due to these properties, XOR operations are widely used in computers and are often used in the following scenarios:

  1. Swap the values ​​of two variables without requiring additional temporary variables.
  2. Sort the elements in an array without using extra space.
  3. Quickly determine whether two numbers are equal. Due to the property of a^b = 0, it can be used to determine whether two numbers are equal.

In programming, XOR operation is usually used to solve some problems of bit operations and low-level computer operations. It is also one of the common techniques to solve some algorithmic problems.

OR operation, AND operation

The OR operation (OR, Bitwise OR) and the AND operation (AND, Bitwise AND) are two binary operations that apply to each bit of two binary numbers. Their operation rules are as follows:

  1. Or operation (|): As long as one of the two binary bits is 1, the result is 1, otherwise the result is 0.

    • For example: 0b1101 | 0b1010 = 0b1111
  2. AND operation (&): Both binary bits must be 1 before the result is 1, otherwise the result is 0.

    • For example: 0b1101 & 0b1010 = 0b1000

OR operations and AND operations are widely used in computers and are often used in the following scenarios:

  1. Bit mask: Through AND and OR operations, specific bits of a number can be masked to extract or set specific bit information.
  2. Conditional judgment: You can use AND operations and OR operations to achieve the effect of conditional judgment, such as judging whether to perform certain operations or set certain flags based on a certain condition.
  3. Bit operations: AND operations and OR operations are the basic operations of bit operations, which can be used to perform various bit-level operations in binary numbers, such as shifting, negation, etc.

In programming, OR operations and AND operations are often used to solve some bit operation problems, or to control and operate the bits of binary numbers. They are the basis for the underlying operations of computers and are also commonly used tools in some algorithms and data processing.

Application of XOR

For example, for the array [2, 3, 4, 2, 4, 5, 3], we can do the following:

  1. Initialize a variable result to 0.
  2. Perform the result ^= num operation on each number num in the array, that is, perform the XOR operation on result and num, and assign the result to result.
  3. The final value of result is the number that appears only once.

Python code example:

def find_single_number(nums):
    result = 0
    for num in nums:
        result ^= num
    return result

# 示例
nums = [2, 3, 4, 2, 4, 5, 3]
print(find_single_number(nums))  # 输出:5

Through the XOR operation, we can efficiently find the numbers that appear only once in the array without using additional data structures or complex traversal methods.

and operation, or application of operation

Here are two examples of algorithms using OR and AND operations:

  1. Bit Masking:
    Bit masking is a technique of masking specific bits of a number using AND and OR operations. By setting certain bits to 1 or 0, we can mask or extract the required information.

Example: Use a bit mask to extract the lower 16 bits and upper 16 bits of a 32-bit integer.

def extract_low_and_high_bits(num):
    low_bits = num & 0xFFFF  # 0xFFFF表示16个二进制位全为1,其余位全为0
    high_bits = (num >> 16) & 0xFFFF
    return low_bits, high_bits

# 示例
number = 0b11001100110011001100110011001100  # 一个32位整数
low, high = extract_low_and_high_bits(number)
print("低16位:", bin(low))
print("高16位:", bin(high))
  1. Determine odd and even numbers:
    For a binary number, its last bit is 1 to indicate an odd number, and 0 to indicate an even number. Therefore, we can use the AND operation to determine whether an integer is odd or even.

Example: Use the AND operation to determine whether an integer is odd or even.

def is_even_or_odd(num):
    if num & 1 == 0:
        return "偶数"
    else:
        return "奇数"

# 示例
number = 10
print(number, "是", is_even_or_odd(number))

These examples show the application of OR and AND operations in algorithms. They can help us perform bit-level operations and judgments and are common techniques for solving some binary number problems.

Guess you like

Origin blog.csdn.net/qq_40140808/article/details/131884214