Introduction to the use of bitwise XOR operators in C ++

Source: Internet
Author: User

The two values involved in the calculation. If the two corresponding bits are the same, the result is 0. Otherwise, the result is 1. 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 1 ^ 1 = 0

Example: 10100001 ^ 00010001 = 10110000

0 ^ 0 = 0 0 ^ 1 = 1 0 variance or any number = any number

1 ^ 0 = ^ 1 = 0 1 variance or any number-returns the inverse of any number

Set yourself to 0 for any number difference or yourself =

(1)Bitwise exclusive or can be used to flip certain specific bits, such as the 10100001 and 2nd bits of the logarithm 3rd, and bitwise exclusive or operation can be performed on the number and 00000110.

10100001 ^ 00000110 = 10100111 // 1010 0001 ^ 0x06 = 1010 0001 ^ 6

(2)By bitwise XOR, two values can be exchanged without using temporary variables. For example, you can use the following statement to exchange the values of two integers, a and B:

A = 10100001, B = 00000110

A = a ^ B; // a = 10100111

B = B ^ a; // B = 10100001

A = a ^ B; // a = 00000110

(3)The unique or operator is characterized by the fact that the number a is two different or the same number B (a = a ^ B) is still the original value.

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.