In-depth understanding of bitwise XOR OR operators

Source: Internet
Author: User

Http://www.cnblogs.com/this-543273659/archive/2011/08/30/2159819.html

The two values that participate in the operation, if the two corresponding bit bits are the same, the result is 0, otherwise 1.
That
0^0 = 0,
1^0 = 1,
0^1 = 1,
1^1 = 0
3 Features of bitwise XOR:
(1) 0^0=0,0^1=1 0 xor or any number = any number
(2) 1^0=1,1^1=0 1 XOR or any number--any number taken against
(3) Any number of different or self = put yourself in 0
Several common uses of bitwise XOR:
(1) Turn some specific bits upside down
For example, the 2nd and 3rd bits of the logarithm 10100001 are flipped, and the number can be bitwise XOR or operated with 00000110.
10100001^00000110 = 10100111

(2) The interchange of two values is implemented without the use of temporary variables.
For example, a value of two integer a=10100001,b=00000110 can be exchanged by the following statement:
A = A^b;//a=10100111
b = b^a;//b=10100001
A = A^b;//a=00000110

(3) In assembly language is often used to set the variable 0:
XOR A,a

(4) quickly determine whether two values are equal
Example 1: To determine whether the two integers a and b are equal, it can be implemented by the following statements:
Return ((a ^ b) = = 0)

For example, the implementation of the initial ipv6_addr_equal () function in 2: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 take advantage of bitwise XOR or achieve a quick comparison, 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 a bitwise XOR on two expressions.

result = expression1 ^ expression2

Parameters
Result

Any variable.

Expression1

Any expression.

Expression2

Any expression.

Description
The ^ operator looks at the value of the binary notation of two expressions and performs a bitwise XOR. The result of this operation is as follows:

0101 (expression1) 1100 (expression2)----1001 (results) if and only if there is a bit on one expression that is 1 o'clock, the result of that bit is 1. Otherwise the result of this bit is 0.

Can only be used for integers


The following procedure uses the bitwise XOR operator:

Class E
{public static void main (String args[])
{
Char a1= ' ten ', a2= ' point ', a3= ' into ', 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:" +A1+A2+A3+A4);
}
}

It's encryption, decryption.

The char type, that is, the character type is actually shaping, or numbers.

All the information in the computer is an integer, all integers can be represented as binary, in fact the computer only knows the binary.
A bitwise operation is a binary integer operation.
Two numbers bitwise XOR or meaning is starting from the single digit, the ratio of a one.
If the two numbers correspond to the same bit, the result is 0, not the same is 1
So 111^101=010
The process of encryption is the secret character-by-character XOR operation.
The decryption process is the ciphertext and the same character XOR or operation.
010^101=111
As for why the ciphertext again or change the original text, this slightly want to know.

Category: C/+ + notes

In-depth understanding of bitwise XOR OR operators

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.