∧: The function of the bitwise operator XOR "C"

Source: Internet
Author: User
Tags bitwise

Transferred from: http://blog.sina.com.cn/s/blog_74a459380101d6xl.html

The XOR operator, ∧, is also called. The rule is that if the two binary numbers of the operation are the same, the result is 0 (false); The XOR is 1 (true). That is 0∧0=0,0∧1=1,1∧1=0. such as:   071∧052, the result is 023 (octal number).

"XOR" means to determine whether the two corresponding bit values are "XOR", for "XOR" (the value is different) to take the true (1), otherwise (0).

The following examples illustrate the application of the ∧ operator:

  (1) Turn specific bits upside down

Let's say 01111010, want to flip it down 4 bits, i.e. 1 to 0 and 0 to 1. It can be ∧ with 00001111 , which is

  

The lower 4 bits of the resulting value are just the flip of the low 4 bits of the original number. Which of the several flips will be set to 1 for the ∧ operation. This is because a bit with a value of 1 in the original number and 1 for the ∧ operation 0, the bit value in the original number 0 and 1 for the ∧ operation results in 1.

  (2) with 0 phase ∧, retain the original value

such as 012∧00=012

  

Because the original number of 1 and 0 ∧ operation to 1,0∧0 0, so the original number is preserved.

  (3) Exchange two values without temporary variables

If a=3,b=4. To swap the values of a and B, you can use the following assignment statements:

  A=a∧b;
B=b∧a;
A=a∧b;

You can use the following vertical to illustrate:

This is equivalent to the following two steps:

① execution of the first two assignment statements: "A=A∧B;" and "B=B∧A;" Equivalent to B=b∧ (A∧B). and B∧a∧b equals A∧b∧b. The result of B∧b is 0, because the same number is ∧ with itself, and the result must be 0. So the value of B is equal to a∧0, which is a, and its value is 3.

② executes a third assignment statement: A=a∧b. Since the value of a is equal to (A∧B), the value of B is equal to (b∧a∧b), so the equivalent of A=a∧b∧b∧a∧b, that is, the value of a equals a∧a∧b∧b∧b, equals B.

A gets the original value of B.

////////////////////////////////////////////////////////////////////////

In binary value operations: The difference is 1, the same is 0, such as 1001 xor or 1010 equals 0011.
XOR is also called a half-plus operation, whose algorithm is equivalent to binary addition without carry: Binary with 1 for true, 0 for false, then XOR algorithm is: 0 XOR or 0=0,1 xor or 0=1,0 xor or 1=1,1 xor or 1=0 (same as 0, 1), these laws are the same as addition, but without carry.

∧: The function of the bitwise operator XOR "C"

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.