The bitwise AND, or, not, exclusive or

Source: Internet
Author: User
Tags bitwise operators
Bitwise operators include: & (bitwise AND), | (bitwise OR), ^ (bitwise OR), and ),~ (Bitwise inversion ).

The priority ranges from high to low ~ , &, ^, |

Bitwise AND operation have two typical usage: one is to take a certain bit of the information of a single bit string, as shown belowCodeMinimum 7 digits of X: X & 0177. Second, let a variable retain a certain number of digits, and the remaining position is 0. For example, the following code keeps X at least 6 digits: x = x & 077.

A typical usage of bitwise OR operations is to set the location of a single bit string to 1. If you want to obtain the rightmost 4 bits as 1, the other bits must be the same as the other bits of the variable J. You can use the logic or operation 017 | J;

A typical usage of bitwise exclusive or operation is to reverse the information of a single bit string. For example, if you want to reverse the information of the rightmost 4 bits of the integer variable J, you can use the logic XOR operation 017 ^ J to reverse the information of the rightmost 4 bits of J, that is, the first bits, the result is 0, and the original value is 0. The result is 1. Exchange two values without temporary variables, for example, a = 3, B = 4. To swap values of A and B, use the following value assignment statement:
A = a ^ B; B = B ^ A; A = a ^ B;

Inverse operations are often used to generate constants irrelevant to system implementations. To set the minimum position of variable X to 0, the remaining digits remain unchanged, use the code X = x &~ 077.

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.