C + + use of bitwise XOR OR operator Introduction _c language

Source: Internet
Author: User
Tags bitwise

The two values that participate in the operation, or 1 if the corresponding digits are the same, the result is 0. namely: 0^0=0, 1^0=1, 0^1=1, 1^1=0

For example: 10100001^00010001=10110000

0^0=0,0^1=1 0 XOR or any number = any number

1^0=1,1^1=0 1 XOR or any number-any number of counter


Any number of differences or own = put yourself 0

(1) bitwise XOR or can be used to flip certain bits, such as the 2nd and 3rd digits of the logarithm 10100001, and the number and 00000110 can be bitwise XOR.


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

(2) the exchange of two values can be achieved by bitwise XOR, without the use of temporary variables. For example, an exchange of two integer a,b values can be achieved by using the following statement:


a=10100001,b=00000110

A=a^b;//a=10100111.

B=b^a;//b=10100001.

A=a^b;//a=00000110.

(3) the difference or operator is characterized in that the number a two times or the same number B (A=a^b^b) is still the original value A.

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.