# Bitwise AND, OR, XOR, or equal arithmetic methods

Source: Internet
Author: User
Tags bitwise

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 bitwise and arithmetic in the form of complement.

Special use of "with operations":

(1) 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.

(2) 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 the lower 4 bits of x, which can be obtained by x & 0000 1111 = 0000 1110;

can also be used to take 2, 4, 6 bits of x.

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 participate in a bitwise OR operation in the complement form.

"OR operation" special effect:

(1) often used for some position 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":

(1) to make a specific bit flip to find a number, corresponding to the X to flip you, the corresponding bit of the number is 1, the rest of the bit is 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.

(2) differ from 0 or, retain the original value, X ^ 0000 0000 = 1010 1110.

This can be clearly seen from the example above.

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.

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.

left shift operator (<<)

Move each bits of an operand to the left several bits (left bits discarded, 0 on the right).

Example: A = a << 2 move the bits of a to the left 2 bits, right 0,

Move left 1 bit after a = a * 2;

If the left-hand side does not contain 1, then each left-shift is equal to the number multiplied by 2.

Right shift operator (>>)

All bits of a number are shifted to the right by a number of digits, positive left 0, negative left 1, and right discard.

The operand is shifted one bit to the right, which is equal to the number divided by 2.

For example: A = a >> 2 move the bits of a to the right 2 bits,

Left complement 0 or 1 depends on whether the number of shifts is positive or negative.

The >> operator shifts all bits of expression1 to the right expression2 The specified number of digits. expression1 The sign bit is used to fill the left empty bit after the right shift. The bits that are moved to the right are discarded.

For example, after the following code is evaluated, the value of temp is-4:

-14 (that is, binary 11110010) right shifts two bits equals-4 (that is, binary 11111100).

var temp = -14 >> 2

Unsigned Right shift operator (>>>)

>>> The expression1 operator shifts the individual bits of the expression2 to the right by The specified number of digits. The left-vacated bits after the right shift are filled with zero. Move out the right bit is discarded.

For example: var temp = -14 >>> 2

The value of the variable temp is -14 (that is, 11111111 11111111 11111111 11110010 in binary), and two bits to the right 1073741820 (i.e. 00111111 11111111 11111111 11111100 in binary).

Compound assignment operator

The bitwise operators are combined with the assignment operators to form a new compound assignment operator, which is:

&= Example: a &= b equals a=a & B

|= Example: a |= b equals a=a | b

>>= Example: a >>= b equals a=a >> b

<<= Example: a <<= b equals a=a << b

^= Example: a ^= b equals a=a ^ b

The arithmetic rules are similar to the arithmetic rules of the compound assignment operators mentioned earlier.

bit operations with different lengths of data

If two different lengths of data are bit-calculated , the system aligns the two to the right and then the bitwise Operation .

Take the "and" operation as an example: we know that in C , the long type is 4 bytes, int is 2 bytes, if a long data with an int data "and" operation, right-side alignment, the left side of the insufficient bit according to the following three cases to complement,

(1) If the integer data is positive, the left side is 16 0.

(2) If the integer data is negative, the left side is 16 1.

(3) If the shaping data is an unsigned number, the left side also complements 16 0.

such as: Long A=123;int b=1; calculate A & B.

such as: Long A=123;int b=-1; calculate A & B.

such as: Long a=123;unsigned int b=1, calculate A & B.

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

Bitwise AND, OR, Xor arithmetic (RPM)

Related Keywords:

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.