Bitwise AND, Or, exclusive or operation

Source: Internet
Author: User
ArticleDirectory
    •  
    •  
    •  
    •  
    •  
    •  

 

Directory (?)[-]

    1. Bitwise AND operator
    2. Bitwise OR operator
    3. XOR Operators
    4. Inverse Operators
    5. Left Shift Operator
    6. Right Shift Operator
    7. Compound assignment operator
    8. Bitwise operations on data of different lengths
Bitwise AND operator (&)

The two data involved in the operation perform the "and" Operation in binary bits.

Calculation rule: 0 & 0 = 0; 0 & 1 = 0; 1 & 0 = 0; 1 & 1 = 1;

That is, if the two digits are "1" at the same time, the result is "1". Otherwise, the value is 0.

For example, 3 & 5 means 0000 0011 & 0000 0101 = 0000 0001. Therefore, 3 & 5 is worth 1.

 

In addition, negative numbers are involved in bitwise AND computation in the form of supplementary codes.

Special uses of "and operations:

(1) Clear. If you want to clear a unit, even if all its binary bits are 0, the result is zero as long as it is a numerical phase that is zero to everyone.

 

(2) locate with one count finger

Method: Find a number, corresponding to the bit to be taken by X. The corresponding bit of this number is 1, and the remaining bit is zero. This number and X are "and operated" to obtain the position in X.

For example, set X = 10101110,

Take the 4 lower bits of X, and use X & 0000 1111 = 0000 1110;

Also availableTake 2, 4, and 6 digits of X.

 

Bitwise OR operator (|)

The two objects involved in the operation perform the "or" Operation in binary bits.

Calculation rules: 0 | 0 = 0; 0 | 1 = 1; 1 | 0 = 1; 1 | 1 = 1;

That is, if one of the two objects involved in the operation is 1, the value is 1.

For example, 3 | 5 is 0000 0011 | 0000 0101 = 0000 0111. Therefore, 3 | 5 is worth 7.

 

In addition, negative numbers are involved in bitwise OR operations in the form of supplementary codes.

Special Functions of "or operation:

(1) It is often used for some locations of a data.

Method: Find a number, corresponding to the bit where X is set to 1, the corresponding bit of this number is 1, and the remaining bit is zero. This number is in the phase of X or enables some position 1 in X.

For example, you can use X | 10100000 0000 1111 = 1010 1111 to get the value at 4-Position 1.

 

XOR operator (^)

The two data involved in the operation perform the "XOR" operation based on the binary bit.

Calculation rules: 0 ^ 0 = 0; 0 ^ 1 = 1; 1 ^ 0 = 1; 1 ^ 1 = 0;

That is, the two objects involved in the operation. If the two corresponding bits are "different" (with different values), the result of this bits is 1; otherwise, it is 0.

 

Special Functions of "exclusive or operation:

(1) Let the specific positioning flip find a number, corresponding to everyone who wants to flip X, the corresponding bit of this number is 1, the remaining bit is zero, this number is different from the corresponding bit of X or.

For example, x = 10101110, so that X is flipped 4 bits low, which can be obtained by x ^ 0000 1111 = 1010 0001.

 

(2) It is different from 0 or retains the original value, x ^ 0000 0000 = 1010 1110.

You can see this point clearly in the above example.

Inverse Operator (~)

The bitwise operation is performed on a piece of data involved in the operation.

Calculation rules :~ 1 = 0 ;~ 0 = 1;

That is, the bitwise inversion of a binary number changes from 0 to 1 and from 0.

 

To make the bitwise of a number zero, it can be expressed as: &~ 1.

~ The value of 1 is 1111111111111110, and then the "and" operation is performed. The bitwise must be 0. Because "~" The priority of operators is higher than that of Arithmetic Operators, Relational operators, logical operators, and other operators.

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, A = A <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = A * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.

Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, A = A> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

 

 

> OperatorExpression1To the rightExpression2The specified number of digits.Expression1Is used to fill the left blank after right shift. The position removed from the right is discarded.

For exampleCodeAfter being evaluated,TempThe value is-4:

-14 (that is, 11110010 of the binary) shifts the right two to 4 (that is, 11111100 of the binary ).

 VaR Temp =-14> 2

 

 

Unsigned right shift operator (>>>)

 

 

>>> OperationOperatorExpression1To the rightExpression2The specified number of digits. After the right shift, the left blank space is filled with zero. The bit removed from the right is discarded.

For example:VaR Temp =-14 >>> 2

VariableTempValueIs-14 (that is, the binary 11111111 11111111 11111111 11110010), shifts two places to the right and then equals to 1073741820 (that is, the binary 00111111 11111111 11111111 ).

Compound assignment operator

Bitwise operationCombine with the value assignment operator to form a new compound value assignment operator, which is:

& = Example: A & = B is equivalent to a = A & B

| = Example: A | = B is equivalent to a = A | B

>>= Example: A >>> = B is equivalent to a = A> B

<= Example: A <= B is equivalent to a = A <B

^ = Example: a ^ = B is equivalent to a = a ^ B

Calculation rules: similar to the preceding calculation rules for compound assignment operators.

Data of different lengths Bitwise operation

If two data entries of different lengths are usedBitwise operationThe system will align the two to the right and thenBitwise operation.

Take the "and" operation as an example to describe the following:C LanguageThe long type occupies 4 bytes, And the int type occupies 2 bytes. If a long type of data is "and" an int type of data, after alignment on the right side, the missing bits on the left are supplemented by the following three conditions,

(1) If the integer data is positive, add 16 zeros on the left.

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

(3) If the integer data is unsigned, 16 zeros are added on the left.

For example: Long A = 123; int B = 1; Calculate A & B.

 

For example: Long A = 123; int B =-1; Calculate A & B.

 

For example: Long A = 123; unsigned int B = 1; Calculate A & B.

 

From: http://blog.csdn.net/21aspnet/article/details/7387373

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.