C XOR or Operation C XOR or operation symbol _c language

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

and Operation:&
Both are 1 to 1, otherwise 0

1&1=1, 1&0=0, 0&1=0, 0&0=0


Or operation: |
Both are 0 to 0, otherwise 1
1|1 = 1, 1|0 = 1, 0|1 = 1, 0|0 = 0


Non-operation: ~
1 Take 0, 0 take 1
~ = 0, ~0 = 1
~ (10001) = 01110


XOR or operation
Equal to 0, unequal to 1.
1^1=0, 1^0=1, 0^1=1, 0^0=0


The following is a detailed explanation:

bit Operations

The operation component of the bit operation can only be integer or character data, and the bitwise operation regards the Operation object as a bit string information composed of two digits, completes the specified operation by bit, and obtains the result of the bit string information.

bitwise operators are:

& (by-bit and), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise reversed).
Where the bitwise negation operator is the monocular operator, and the rest is the binocular operator.
Bitwise operators are prioritized from highest to lowest, followed by ~, &, ^, |,
Where the union direction is from right to left and the precedence is higher than the arithmetic operator, the remaining operators are bound from left to right, and the precedence is lower than the relational operator.

(1) Bitwise AND operator (&)

Bitwise-and-operation calculates the corresponding bits of two operational components in accordance with the following rules:
0 & 0 = 0, 0 & 1 = 0, 1 & 0 = 0, 1 & 1 = 1.
The same is 1 bit, the result is 1, otherwise the result is 0.
For example, the internal representation of 3 is
00000011
The internal representation of 5 is
00000101
The result of the 3&5 is
00000001
There are two typical uses for bitwise AND operation, one is to take a bit of information from a string, such as the following code intercept x's minimum 7 digits: X & 0177. The second is to keep a certain number of bits, the remaining position 0, such as the following code to keep x only the minimum 6 digits: x = x & 077. The above use is to design a constant, the constant only needs a bit is 1, the unwanted bit is 0. Use it to bitwise with the specified bit string information.

(2) Bitwise OR operator (|)

A bitwise OR operation calculates the corresponding bits of the two operational components according to the following rules:
0 | 0 = 0, 0 | 1 = 1, 1 | 0 = 1, 1 | 1 = 1
That is, as long as there is a bit of 1, the result is 1, otherwise 0.
For example, 023 | 035 The result is 037.
A typical use of bitwise OR operation is to place a certain position of a bit string information into 1. If you are going to get the right 4 to 1 and the other bits are the same as the other bits of the variable J, you can use logical or operational 017|j. To assign the result to a variable J, you can write:
j = 017|j

(3) Bitwise XOR or operator (^)

The corresponding bits of two operational components are evaluated by bitwise XOR according to the following rules:
0 ^ 0 = 0, 0 ^ 1 = 1, 1 ^ 0 = 1, 1 ^ 1 = 0
That is, the value of the corresponding bit is the same, the result is 0, and the result is 1.
For example, the 013^035 result is 026.
The meaning of the XOR operation is to find whether the corresponding bit value of two components is different, the difference is 1, the same is 0. A typical use of bitwise XOR is the negation of some bits of information in a bit string. If you desire integer variable J of the right 4-bit information, with logical XOR or Operation 017^j, you can get J the right 4 bits of information, that is, the original 1 bit, the result is 0, originally 0 of the bit, the result is 1.

(4) Bitwise counter operator (~)

A bitwise inverse operation is a monocular operation that is used to find the inverse of a bit string information, that is, which is 0 bit, the result is 1, and which is the 1 bit, and the result is 0. For example, the result of ~7 is 0XFFF8.

Inverse operations are often used to generate constants independent of system implementation. To change the variable x to a minimum of 6 positions to 0, the remaining bits are unchanged and can be implemented using code x = x & ~077. The above code is independent of the integer x 2 bytes or 4 byte implementation.

When two different lengths of data are bitwise operations (such as Long data and int data), the right end of the two component is aligned to bitwise operations. If the short number is positive, the high position is filled with 0; If the short number is negative, the high position is filled with 1. If the short is an unsigned integer, the high position is always filled with 0.

Bit operation is used to compute the bit string information and to get the result of the bit string information. If the following code can remove the bit string information of the integer variable k, the information bit to the right of 1: ((k-1) ^k) & K.

Shift Operations

A shift operation is used to move integral or character data as a whole as a binary information string. There are two operators:
<< (move left) and >> (move right)
The shift operation is binocular operation, there are two operation components, the left component is the shift data object, the right component value is the shift bit number. The shift operation regards the left operation component as the bit string information consisting of binary, and shifts to the left or right to get the new bit string information.
The shift operators have precedence over arithmetic operators, which are higher than the relational operators, and they are bound from left to right.
(1) left-shift operator (<<)
The left-shift operation moves one bit of string information to the left with the specified bit, and the empty bits at the right end are supplemented by 0. For example 014&LT;&LT;2, the result is 060, or 48.
When left, the right end of the space is supplemented by 0, and the information of the bits removed from the left is discarded. In a binary number operation, 1 bits per left is equal to 2 in case the information is not lost due to movement. such as 4 << 2, the result is 16.
(2) Right shift operator (>>)
The right shift operation moves a bit string information to the right by the specified bit, and the bits removed from the right end are discarded. For example 12&GT;&GT;2, the result is 3. As opposed to left, for small integers, move 1 bits to the right, which is equal to dividing by 2. When you move to the right, you need to be aware of the sign bit problem. For unsigned data, the left blank bits are added in 0 when the right shift is made. For signed data, if the sign bit is 0 (positive) before the shift, then left is also supplemented with 0, and if the sign bit is 1 (negative) before the shift, the left is supplemented with 0 or 1, depending on the computer system. For negative numbers to the right, call the 0-plus system as "logical Right", with 1-Plus system for "arithmetic right shift". The following code illustrates the right shift method used by the reader on the computer system:
printf ("%d\n\n\n", -2>>4);
If the output is 1, the arithmetic right shift is used, and the output is a large integer, then the logical right shift.
The combination of shift operation and bitwise operation can realize many complex computations related to bit-string operation. Sets the number of bits from right to left in the variable, from 0 bits to 15 digits, and the expression to be positioned is a positive integer of no more than 15. The following codes have the meanings shown in their right annotations:
~ (~0 << N)
(x >> (1 p-n)) & ~ (~0 << N)
New |= (Old >> Row) & 1 << (15–k)
S &= ~ (1 << j)
for (j = 0; ((1 << J) & s) = = 0; j);
===================================================================================================
Bitwise operations are operations that are performed in binary order. In system software, it is often necessary to deal with bits problems. The C language provides 6 bit operator operators. These operators can only be used for integer operands, that is, only for signed or unsigned char,short,int and long types.

The list of bitwise operators provided by the C language:

Description of operator meaning
& Bitwise AND if two corresponding bits are 1, the result value of this bit is 1, otherwise 0
| A bitwise or two corresponding bits as long as one is 1, the result value of this bit is 1
^ Bitwise XOR or if the two bits values are equal to 0, otherwise 1
~ is a unary operator, which is used to reverse a binary number by a bit, which is about 0 to 1, and 1 to 0.
<< left to move a number of bits all left n bit, right 0
>> Right Move a number of each bits to the right n bit, move to the right side of the low is discarded, for unsigned number, high 0

1, "Bitwise AND" operator (&)

    bitwise AND refers to: two data that participates in the operation, and the "and" operation is done by bits. If two corresponding bits are 1, the result value of the bit is 1; otherwise, 0. The 1 here can be understood as true,0 in logic that can be interpreted as false in logic. Bitwise AND is in fact consistent with the logical "and" Operation rules. The logic of "and", the requirements of the full truth, the results are true. If, a=true,b=true, then a∩b=true for example: 3&5 3 binary encoding is 11 (2). (to differentiate between decimal and other systems, this article stipulates that all non-decimal data is appended to the data followed by parentheses, which are marked in parentheses, and binary is labeled 2 the base unit of the memory-stored data is byte (byte) and a byte consists of 8 bits (bit). A bit is the smallest unit used to describe the amount of data in a computer. In binary systems, each 0 or 1 is a bit. The 11 (2) is made up to one byte, then 00000011 (2). The binary encoding of 5 is 101 (2) and complements it to one byte, then 00000101 (2)
Bitwise AND Operation:
00000011 (2)
&00000101 (2)
00000001 (2)
3 &5=1
C language code:
#include <stdio.h>
Main ()
{
int a=3;
int b = 5;
printf ("%d", a&b);
}
Bitwise Purpose:
(1) 0
If you want to clear zero for a storage unit, even if all of its bits is 0, just find a binary number where each bit meets the criteria:

The original number is 1 bit, the corresponding bit in the new number is 0. Then make the two & operations, you can achieve the purpose of the Qing 0.
Example: The original number is 43, that is 00101011 (2), another number, set it to 148, that is 10010100 (2), the two bitwise AND operation:
00101011 (2)
&10010100 (2)
00000000 (2)
C Language Source code:
#include <stdio.h>
Main ()
{
int a=43;
int B = 148;
printf ("%d", a&b);
}
(2) To take certain points in a number of positions
If you have an integer a (2byte), you want to take the lower byte, you only need to put a and 8 1 bitwise and can.
A 00101100 10101100
B 00000000 11111111
C 00000000 10101100
(3) retention refers to positioning:
With a number of "bitwise AND" operations, this number is 1 in that bit.
For example, a number of 84, or 01010100 (2), would like to retain the first 3,4,5,7,8 bit from the left, as follows:
01010100 (2)
&00111011 (2)
00010000 (2)
namely: a=84,b=59
C=a&b=16

C Language Source code:
#include <stdio.h>
Main ()
{
int a=84;
int B = 59;
printf ("%d", a&b);
}


2, "bitwise OR" operator (|)

In two corresponding bits, as long as one is 1, the result value of this bit is 1. In the case of logic or arithmetic, one is true.


For example: 60 (8) |17 (8), octal 60 and octal 17 are bitwise OR operations.
00110000
|00001111
00111111
C Language Source code:
#include <stdio.h>
Main ()
{
int a=060;
int b = 017;
printf ("%d", a|b);
}
Application: A bitwise OR operation is commonly used to set a certain bit value of 1 for a data. For example, if you want to change the lower 4 bits of a number A to 1, you only need to bitwise or operate A and 17 (8).

3, Exchange two values, no temporary variables

For example: A=3, namely one (2); b=4, that is, 100 (2).
To swap the values of a and B, you can implement the following assignment statement:
A=a∧b;
B=b∧a;
A=a∧b;
a=011 (2)
(∧) b=100 (2)
a=111 (2) (Result of A∧b, A has become 7)
(∧) b=100 (2)
b=011 (2) (Result of B∧a, B has become 3)
(∧) a=111 (2)


A=100 (2) (Result of A∧b, A has become 4)
is equivalent to the following two steps:
① executes the first two assignment statements: "A=a∧b" and "b=b∧a;" are equivalent to B=b∧ (A∧B).
② to execute the third assignment statement: A=a∧b. Because the value of a is equal to (A∧B), the value of B equals (b∧a∧b),

Therefore, the equivalent of A=a∧b∧b∧a∧b, that is, the value of a is equal to a∧a∧b∧b∧b, equal to B.
It's amazing!
C Language Source code:

Copy Code code as follows:

#include <stdio.h>
Main ()
{
int a=3;
int b = 4;
A=a^b;
B=b^a;
A=a^b;
printf ("a=%d b=%d", a,b);
}


4, "Take Back" operator (~)

He is a unary operator, which is used to find the binary inverse code of integers, that is, to change 1 of the operands on each bits to 0, and 0 to 1.
For example: ~77 (8)
Source:
#include <stdio.h>
Main ()
{
int a=077;
printf ("%d", ~a);
}


5, left shift operator (<<)

The left-shift operator is used to move the bits of a number to the left several digits, and the number of bits to be moved is specified by the right operand (right operand must be non-negative

Value, the left side of the space is filled with 0, and the high shift overflow discards the high.
For example: Move the binary number of a to the left 2 digits, the right space out of the bit 0, the left overflow bit discarded. If a=15, that is 00001111 (2), move left 2

Bit 00111100 (2).
Source:
#include <stdio.h>
Main ()
{
int a=15;
printf ("%d", a<<2);
}
The 1-bit left is equal to the number multiplied by 2, and the left 2-bit equivalent to the number multiplied by 2*2=4,15<<2=60, that is, by 4. But this conclusion only applies to the

When the number is left, the overflow discards the high position that does not contain 1.
Suppose that an integer is stored in one byte (8 bits), and if A is an unsigned integer variable, then when a=64, the left one when it overflows is 0

, while moving left 2 digits, the overflow high contains 1.


6, right shift operator (>>)
The right shift operator is used to shift the bits of a number to the right, and the number of bits to move is specified by the right operand (right operand must be non-negative

Value, the lower end of the move to the right is discarded, and for unsigned numbers, the high position is 0. For signed numbers, some machines will empty the left part of the

Fill (i.e. "arithmetic shift") with sign bits, while others fill (i.e. "logically shift") the parts vacated by the left. Note

Meaning: To unsigned number, move right when the left high position into 0; for signed values, if the original symbol bit is 0 (the number is positive), the left is also moved

Into the 0. If the symbol bit turns out to be 1 (that is, a negative number), move the left to 0 or 1, depending on the computer system you are using. Some systems move into 0, some

The system moves into 1. The move into 0 is called "logical shift", that is, the simple shift, and the move into 1 is called "Arithmetic shift."
Example: The value of a is octal number 113755:
A:1001011111101101 (represented in binary form)
a>>1:0100101111110110 (when logic moves right)
a>>1:1100101111110110 (when arithmetic moves right)
In some systems, A>>1 gets octal number 045766, while in other systems it is possible to get 145766. Turbo C and some other C

The compilation takes the arithmetic right shift, that is, when the symbol number is shifted to the right, if the sign bit is 1, the left is 1.
Source:
#include <stdio.h>
Main ()
{
int a=0113755;
printf ("%d", a>>1);
}


7, bitwise operation Assignment operator

Bitwise operators and assignment operators can compose compound assignment operators.
For example: &=, |=, >>=, <<=,∧=
Example: A & = b equals A = A & B
A << = 2 equals a = a << 2

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.