In-depth understanding of bitwise XOR Operators

Source: Internet
Author: User

 

In-depth understanding of bitwise XOR Operators

The two values involved in the operation. If the two bits are the same, the result is 0. Otherwise, the result is 1.
That is:
0 ^ 0 = 0,
1 ^ 0 = 1,
0 ^ 1 = 1,
1 ^ 1 = 0
Three Characteristics of bitwise XOR:
(1) 0 ^ 0 = 0 ^ 1 = 1 0 exclusive or any number = any number
(2) 1 ^ 0 = ^ 1 = 0 1 exclusive or any number-inverse of any number
(3) Any data difference or oneself = set oneself to 0
Common usage of bitwise XOR:
(1) Flip certain specific bits
For example, if the logarithm is 10100001 bits and 2nd bits are flipped, bitwise exclusive or operations can be performed on the number and 3rd bits.
10100001 ^ 00000110 = 10100111

(2) Exchange two values without using temporary variables.
For example, you can use the following statement to exchange values of two integers, A = 10100001 and B = 00000110:
A = a ^ B; // A = 10100111
B = B ^ A; // B = 10100001
A = a ^ B; // A = 00000110

(3) In assembly languages, it is often used to set variables to zero:
XOR a,

(4) quickly determine whether two values are equal
Example 1: To determine whether two integers A and B are equal, use the following statement:
Return (a ^ B) = 0)

Example 2: the implementation of the initial functions 6_addr_equal () 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]);
}

You can use the bitwise variance or quick comparison. The latest implementation has been changed:
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 generic:

Perform bitwise XOR or on two expressions.

Result = expression1 ^ expression2

Parameters
Result

Any variable.

Expression1

Any expression.

Expression2

Any expression.

Description
^ Operator to view the values of the two expressions in binary notation and perform bitwise XOR. The result of this operation is as follows:

0101 (expression1) 1100 (expression2) ---- 1001 (result) When and only when one of the expression is 1, the result is 1. Otherwise, the result is 0.

Can only be used as an integer

 

The following program uses the bitwise XOR operator:

Class E
{Public static void main (string ARGs [])
{
Char a1 = '10', a2 = 'point', A3 = 'in', A4 = 'attacking ';
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:" + A1 + A2 + A3 + A4 );
}
}

Encryption and decryption.

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

All information in the computer is an integer, and All integers can be expressed as binary. In fact, the computer only recognizes binary.
Bitwise operations are binary integer operations.
The two numbers are equal to or mean the ratio of one digit to one digit starting from a single digit.
If the two numbers correspond to the same bit, the result is 0, and the difference is 1.
So 111 ^ 101 = 010
The encryption process is an exclusive or operation between characters one by one and that secret character.
The decryption process is the XOR operation of the ciphertext and the same character.
010 ^ 101 = 111
As for the reason why the ciphertext is changed to the original text again or again, I will know it if I want ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.