Actions related to bitwise AND, OR, XOR, etc.

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise logical operators

Bitwise -AND Operator (&)

Participate in the operation of the two data, press bits for "and" operation.
Arithmetic Rules:0&0=0;    0&1=0;     1&0=0; 1&1=1;
That is, the two-bit is "1" at the same time, the result is "1", otherwise 0
For example: 3&5 is 0000 0011 & 0000 0101 = 0000 0001 Therefore, 3&5 is worth 1.

In addition, negative numbers participate in the bitwise AND operation in the complement form.

Special use:

A: Clear Zero

If you want to clear a unit to zero, even if all of its bits is 0, as long as the value with a everyone is zero and the result is zero.

B: Take a number to locate the middle finger

Method: Find a number, corresponding to the x to take the bit, the corresponding bit of the number is 1, the rest of the bits are zero, this number and X for "and operations" can be obtained by means of X-position.
Example: Set x=10101110, take X's low 4 bits, with x & 0000 1111 = 0000 1110 can be obtained, can also be used to take x 2, 4, 6 bits.

Bitwise OR operator (|)

To participate in the operation of the two objects, press bits for "or" operation.
Operation rules: 0|0=0; 0|1=1; 1|0=1; 1|1=1;

That is, the two objects that participate in the operation have a value of 1 and 1.
Example: 3|5 0000 0011 | 0000 0101 = 0000 0111 Therefore, the 3|5 is worth 7.

In addition, negative numbers are added or calculated in complement form.

Special use:

A: Often used for certain locations of a data 1

Method: Find a number, corresponding to the x to set 1 bits, the corresponding bit of the number is 1, the remaining bits are zero. This number is in X-phase or can make some position in X 1.
Example: Lower 4 position of x=10100000 1, with X | 0000 1111 = 1010 1111 can be obtained.

Xor operator (^)

To participate in the operation of the two data, press bits for "XOR" operation.
Operation rules: 0^0=0; 0^1=1; 1^0=1; 1^1=0;
That is: The two objects participating in the operation, if two corresponding bits are "XOR" (the value is different), then the bit result is 1, otherwise 0.

Special effect of "XOR operation":

A: Turn specific bits upside down

Find a number, corresponding to the X to flip, the corresponding bit of the number is 1, the rest of the bits are zero, this number and x corresponding bit XOR.
Example: x=10101110, make x low 4 bits flip, with x ^ 0000 1111 = 1010 0001 can be obtained.

B: differ from 0 or, retain the original value, X ^ 0000 0000 = 1010 1110

Take the inverse operator (~)

To participate in the operation of a data, press bits for "negation" operation.
Operational rules: ~1=0; ~0=1;
That is: to a binary number in the reverse position, the 0 will change to 0.

The lowest bit of a number is zero, which can be represented as: a&~1.
Explanation: The value of 1111111111111110, and then the "and" operation, the lowest bit must be 0. Because the "~" operator has precedence over arithmetic operators, relational operators, logical operators, and other operators.

The

left-shift operator (<<)
Shifts all bits of an operand to the left by a number of bits (the left bits is discarded and the right is 0).
Example: A = a << 2 moves the bits of a to the left 2 bits, 0 to the right,
to the left 1 digits after a = a * 2;&NBSP;
If the left-hand side of the high-discard does not contain 1, each left-shift is equal to the number multiplied by 2. The
right-shift operator (>>)
shifts all bits of a number to the right number of digits, positive left 0, negative left 1, and right discard. The
operand shifts one bit to the right, which is equal to the number divided by 2.
For example: A = a >> 2 shifts the bits of a to the right 2 bits,
left 0 or 1 to see if the number of shifts is positive or negative.

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.