XOR operation principle and application

XOR, is a mathematical operator, English is exclusive OR, abbreviated as xor, applied to logical operations. The mathematical symbol for XOR is "⊕", and the computer symbol is "xor". Its algorithm is:

  a⊕b = (¬a ∧ b) ∨ (a ∧¬b)

  If the values ​​of a and b are different, the XOR result is 1. If the two values ​​of a and b are the same, the XOR result is 0.

  XOR is also called half-add operation, and its algorithm is equivalent to binary addition without carry: in binary, 1 is used to represent true, and 0 is used to represent false, then the XOR operation algorithm is: 0⊕0=0, 1⊕0= 1, 0⊕1=1, 1⊕1=0 (same as 0, different as 1), these rules are the same as addition, but without carry.

  XOR is abbreviated as XOR, EOR, EX-OR

  There are three operators in the program: XOR, xor, ⊕.

The role of XOR operation

  The two values ​​involved in the operation, if the two corresponding bits are the same, the result is 0, otherwise it is 1.

  Right now:

  0^0 = 0,

  1^0 = 1,

  0^1 = 1,

  1^1 = 0

  Three characteristics of bitwise XOR:

  (1) 0^0=0, 0^1=1 0 XOR any number = any number

  (2) 1^0=1, 1^1=0 1 XOR any number - any number inversion

  (3) Any number XOR itself = set itself to 0

  Several common uses of bitwise XOR:

  (1) Flip some specific bits

  For example, if the 2nd and 3rd digits of the logarithm 10100001 are flipped, then the bitwise XOR operation can be performed between the number and 00000110.

  10100001^00000110 = 10100111

  (2) Realize the exchange of two values ​​without using temporary variables.

  For example, exchanging the values ​​of two integers a=10100001 and b=00000110 can be realized by the following statement:

  a = a^b;   //a=10100111

  b = b^a;   //b=10100001

  a = a^b;   //a=00000110

  (3) Often used in assembly language to set variables to zero:

  xor a,a

  (4) Quickly judge whether two values ​​are equal

  Example 1: To judge whether two integers a and b are equal, it can be realized by the following statement:

  return ((a ^ b) == 0)

Example 2: The implementation of the original ipv6_addr_equal() function in Linux is as follows:

  staTIc inline int ipv6_addr_equal(const struct in6_addr *a1, const struct in6_addr *a2)

  {

  return (a1-》s6_addr32[0] == a2-》s6_addr32[0] &&

  a1-》s6_addr32[1] == a2-》s6_addr32[1] &&

  a1-》s6_addr32[2] == a2-》s6_addr32[2] &&

  a1-》s6_addr32[3] == a2-》s6_addr32[3]);

  }

  Fast comparisons can be implemented using bitwise XOR, and the latest implementation has been modified to:

  staTIc inline int ipv6_addr_equal(const struct in6_addr *a1, const struct in6_addr *a2)

  {

  return (((a1-》s6_addr32[0] ^ a2-》s6_addr32[0]) |

  (a1-》s6_addr32[1] ^ a2-》s6_addr32[1]) |

  (a1-》s6_addr32[2] ^ a2-》s6_addr32[2]) |

  (a1-》s6_addr32[3] ^ a2-》s6_addr32[3])) == 0);

      }

5 Application formula:

  Performs bitwise exclusive-or of two expressions.

  result = expression1 ^ expression2

  parameter

  result

  any variable.

  expression1

  any expression.

  expression2

  any expression.

  illustrate

  The ^ operator looks at the values ​​of the binary representations of two expressions and performs a bitwise exclusive-or. The result of this operation is as follows:

  0101 (expression1) 1100 (expression2) ----1001 (result) If and only if only one expression has a 1 on a bit, the result bit is 1. Otherwise the bit of the result is 0.

  only for integers

  The following program uses the "bitwise XOR" operator:

  class E

  { public staTIc void main(String args[ ])

  {

  char a1='ten', a2='point', a3='in', a4='attack';

  char secret=‘8’ ;

  a1=(char) (a1^secret);

  a2=(char) (a2^secret);

  a3=(char)(a3^secret);

  a4= (char) (a4^secret);

  System.out.println("Ciphertext: "+a1+a2+a3+a4);

  a1=(char) (a1^secret);

  a2=(char) (a2^secret);

  a3=(char)(a3^secret);

  a4= (char) (a4^secret);

  System.out.println("Original text: "+a1+a2+a3+a4);

  }

  }

  Encryption and decryption

  The char type, that is, the character type is actually an integer, which is a number.

  All the information in the computer is an integer, and all integers can be expressed in binary, but in fact, the computer only understands binary.

  Bit operations are operations on binary integers.

  The bitwise XOR of two numbers means starting from the ones digit, one by one.

  If the corresponding bits of the two numbers are the same, the result is 0, and the result is 1 if they are different.

  So 111^101=010

  The encryption process is the XOR operation with the secret character character by character.

  The decryption process is the XOR operation of the ciphertext and the same character

  010^101=111

  As for why the ciphertext becomes the original text once again XORed, this can be understood after a little thought. .

XOR operation: bitwise XOR operator

First of all, XOR means that when the binary representation of two numbers is XORed, the two binary representations of the current bit are different, and the same is 0. This method is widely used to count the number of 1 digits in a number!

  The two values ​​involved in the operation, if the two corresponding bits are the same, the result is 0, otherwise it is 1.

  Right now:

  0^0 = 0,

  1^0 = 1,

  0^1 = 1,

  1^1 = 0

  Three characteristics of bitwise XOR:

  (1) 0^0=0, 0^1=1 0 XOR any number = any number

  (2) 1^0=1, 1^1=0 1 XOR any number - any number inversion

  (3) Any number XOR itself = set itself to 0

  Several common uses of bitwise XOR:

  (1) Flip some specific bits

  For example, if the 2nd and 3rd digits of the logarithm 10100001 are flipped, then the bitwise XOR operation can be performed between the number and 00000110.

  10100001^00000110 = 10100111

  (2) Realize the exchange of two values ​​without using temporary variables.

  For example, exchanging the values ​​of two integers a=10100001 and b=00000110 can be realized by the following statement:

  a = a^b;   //a=10100111

  b = b^a;   //b=10100001

  a = a^b;   //a=00000110

  bit operation

  In the bit operation, after the number is expressed in binary, the operation of adding 0 or 1 to each bit. The first step to understanding bit operations is understanding binary. Binary means that every bit of a number is 0 or 1. For example, 2 in decimal is converted to 10 in binary.

  In fact, binary operations are not difficult to master, because there are only 5 operations in bit operations: AND, OR, XOR, left shift, and right shift. As shown in the following table:

  

  Left shift operation:

  The left shift operator m "" n means that m is left shifted by n bits. When shifting n bits to the left, the leftmost n bits will be discarded, and n 0s will be added to the rightmost. For example:

  

  Right shift operation:

  The right shift operator m》》n means to shift m to the right by n bits. When shifting right by n bits, the rightmost n bits are discarded. But handling the leftmost bit when shifting right is a little more complicated. Special attention should be paid here, if the number is an unsigned value, the leftmost n bits are filled with 0. If the number is a signed value, the leftmost n bits are filled with the sign bit of the number. That is to say, if the number is originally a positive number, n 0s are added to the far left after the right shift; if the number is originally negative, n 1s are added to the far left after the right shift. The following is a heap of two 8-bit signed Example of shifting numbers to the right:

  

  There is an equivalence relation about shift operations: shifting an integer to the right by one bit is mathematically equivalent to dividing an integer by 2.

  

  The computer only recognizes 1 and 0, and the decimal system needs to be converted into binary to use the shift operator 《《》》.

  int j = 8;

  p = j 《《 1;

  cout《《p《《endl;

  Here, shifting 8 to the left by one bit is the result 16 of 8*2.

  The shift operation is one of the most efficient operations for computing multiply/divide multiplications.

  The function of bitwise AND (&) is to AND the corresponding binary bits of the two numbers involved in the operation. Only when the corresponding two binary bits are 1, the result bit is 1, otherwise it is 0. The numbers involved in the operation appear in complement form.

  First give an example as follows:

  Topic: Please implement a function that takes a positive number as input and outputs the number of 1s in the binary representation of the number.

  

  A knowledge point is used here: subtracting 1 from an integer, and then performing an AND operation with the original integer, will change the rightmost 1 of the integer to 0. Then how many 1s there are in the binary representation of an integer, you can perform this operation as many times as possible.

  Summary: After subtracting 1 from an integer and then performing a bit-AND operation with the original integer, the result obtained is equivalent to changing the rightmost 1 in the binary representation of the integer to 0.

  The application of bit operations can be used in many situations:

  Clear a specific bit (the specific position in the mask is 0, other bits are 1, s = s & mask).

  Take a specified bit in a certain number (a specific position in the mask, other bits are 0, s = s & mask).

  Example: Input two integers m and n, and calculate how many bits in the binary representation of m need to be changed to get n.

  Solution: The first step is to find the XOR of these two numbers; the second step is to count the digits of 1 in the XOR result.

  

  Next, let's give another example to better explain the shift operation: use a statement to judge whether an integer is an integer power of 2.

  Solution: If an integer is an integer power of 2, then one and only one bit in its binary representation is 1, and all other bits are 0. According to the previous analysis, after subtracting 1 from this integer and doing an AND operation with itself, the only 1 in this integer becomes 0.

  answer:! (x & (x - 1))

Guess you like

Origin blog.csdn.net/weixin_40593838/article/details/126834641