# Bitwise OR of C language, and bitwise operator of non-operator c Language

Source: Internet
Author: User

Bitwise operators
C provides six bitwise operators. These operators may only allow integer operands, namely char, short, Int, and long, regardless of signed or unsigned.
& By-bit and
| Bitwise OR
^ Bitwise OR
<Move left
> Right shift
~ Inverse (unary operation)
Bitwise AND OPERATION
N = N & 0177;
So that the number of lower 7 digits in N is 0.
Bitwise OR operation | used to open some bits:
X = x | set_on;
Changes some set_on and relative bits of X to 1.
Bitwise OR operation ^ sets this bitwise to 1 and 0 when one of the two operands is different.
The bitwise operators &, | and logical operators &, | are distinguished. The latter evaluates a true value from left to right. For example, if X is 1 and Y is 2, X & Y is 0, and X & Y is 1.
Shift Operator <and> shifts the left operand to the left or the right operand to the specified number. The right operand must be non-negative. Therefore, x <2 moves the value of X to two places on the left and fills the space with 0. This is equivalent to multiplying 4. If you move the value to the right, an unsigned number is filled with 0. Shifts the number of symbols to the right. On some machines, the number of symbols is filled with the symbol bit ("arithmetic shift"), and on other machines, the number is filled with 0 ("logical shift ").
Single Object operator ~ Returns an integer. The bitwise of every 1 is 0 or vice versa. For example
X = x &~ 077
Convert the last six positions of X to 0. Note that X &~ The value of 077 depends on the font length. For example, if X is a 16-digit value, it is X & 0177700. This simple type does not cause additional costs, because ~ 077 is a constant expression that can be calculated during compilation.
As an instance using bit operations, consider the getbits (x, P, n) function ). It returns the n-bit x value starting with P. Let's assume that 0 is at the rightmost, and N and P are positive numbers. For example, getbits (x, 4, 3) returns 4, 3, and 2 digits on the right.
/* Getbits: returns the n-bits starting from position P */
Unsigned getbits (unsigned X, int P, int N)
{
Return (x> (p + 1-N ))&~ (~ 0 <n );
}
Expression x> (p + 1-N .~ 0 is full 1; shift n to left and enter 0 in the rightmost; Use ~ Makes n 1 on the rightmost side a mask.

----------------------------------

Bitwise operators in C Language

& Bitwise AND | the bitwise OR ^ bitwise is different or 1. The bitwise AND operator "&" is a binary operator. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented. For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1. Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ). Main () {int A = 9, B = 5, C; C = A & B; printf ("A = % d/Nb = % d/NC = % d/N", a, B, c);} 2. bitwise OR operator "|" is a binary operator. Its function is the binary phase or corresponding to the two numbers involved in the operation. If one of the two binary numbers is 1, The result bit is 1. The two numbers involved in the operation appear as a complement. Example: 9 | 5 writable formula: 00001001 | 00000101 00001101 (decimal: 13) Visible 9 | 5 = 13 main () {int A = 9, B = 5, C; C = A | B; printf ("A = % d/Nb = % d/NC = % d/N", a, B, c );}

3. bitwise exclusive or operation bitwise exclusive OR operator "^" is a binary operator. This function is used to calculate whether the binary numbers corresponding to the binary numbers are different or not. When the binary numbers of the two numbers are different, the result is 1. The number of involved operations still appears as a complement Code. For example, 9 ^ 5 can be written as follows: 00001001 ^ 00000101 00001100 (12 in decimal format) Main () {int A = 9; A = a ^ 15; printf ("A = % d/N", );}

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

4. bitwise XOR or operator ^ is involved in two operations. If the two corresponding bits are the same, the result is 0. Otherwise, the result is 1. 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 1 ^ 1 = 0 for example: 10100001 ^ 00010001 = 10110000

0 ^ 0 = 0 0 ^ 1 = 1 0 variance or any number = any number

1 ^ 0 = ^ 1 = 0 1 variance or any number-returns the inverse of any number

If you set yourself to 0 (1) based on any number or yourself =, it can be used to flip some specific bits, such as the 10100001 and 2nd bits of the logarithm 3rd, bitwise exclusive or operation can be performed on numbers and 00000110. 10100001 ^ 00000110 = 10100111 // 1010 0001 ^ 0x06 = 1010 0001 ^ 6 (2) exchange two values without using temporary variables by bitwise XOR operation. For example, to exchange the values of two integers A and B, you can use the following statements: A = 10100001, B = 00000110 A = a ^ B; // A = 10100111 B = B ^; // B = 10100001 A = a ^ B; // A = 00000110 (3). In assembly languages, variables are often set to zero:

& Bitwise AND | the bitwise OR ^ bitwise is different or 1. The bitwise AND operator "&" is a binary operator. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented. For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1. Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ). Main () {int A = 9, B = 5, C; C = A & B; printf ("A = % d/Nb = % d/NC = % d/N", a, B, c);} 2. bitwise OR operator "|" is a binary operator. Its function is the binary phase or corresponding to the two numbers involved in the operation. If one of the two binary numbers is 1, The result bit is 1. The two numbers involved in the operation appear as a complement. Example: 9 | 5 writable formula: 00001001 | 00000101 00001101 (decimal: 13) Visible 9 | 5 = 13 main () {int A = 9, B = 5, C; C = A | B; printf ("A = % d/Nb = % d/NC = % d/N", a, B, c );}

3. bitwise exclusive or operation bitwise exclusive OR operator "^" is a binary operator. This function is used to calculate whether the binary numbers corresponding to the binary numbers are different or not. When the binary numbers of the two numbers are different, the result is 1. The number of involved operations still appears as a complement Code. For example, 9 ^ 5 can be written as follows: 00001001 ^ 00000101 00001100 (12 in decimal format) Main () {int A = 9; A = a ^ 15; printf ("A = % d/N", );}

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

4. bitwise XOR or operator ^ is involved in two operations. If the two corresponding bits are the same, the result is 0. Otherwise, the result is 1. 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 1 ^ 1 = 0 for example: 10100001 ^ 00010001 = 10110000

0 ^ 0 = 0 0 ^ 1 = 1 0 variance or any number = any number

1 ^ 0 = ^ 1 = 0 1 variance or any number-returns the inverse of any number

If you set yourself to 0 (1) based on any number or yourself =, it can be used to flip some specific bits, such as the 10100001 and 2nd bits of the logarithm 3rd, bitwise exclusive or operation can be performed on numbers and 00000110. 10100001 ^ 00000110 = 10100111 // 1010 0001 ^ 0x06 = 1010 0001 ^ 6 (2) exchange two values without using temporary variables by bitwise XOR operation. For example, to exchange the values of two integers A and B, you can use the following statements: A = 10100001, B = 00000110 A = a ^ B; // A = 10100111 B = B ^; // B = 10100001 A = a ^ B; // A = 00000110 (3). In assembly languages, variables are often set to zero:

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.