To: c |, |, &, &, exclusive or ,~ ,! Operation

Source: Internet
Author: User
Tags bitwise operators

Conversion from: c |, |, &, &, Or ,~ ,! Operation

Bitwise operation The bitwise operation component can only be integer or numeric type data. bitwise operations regard the operation object as Bit String information consisting of binary digits to complete the specified operation by bit, obtain the Bit String information.Bitwise operators include: & (By bit and), | (by bit or), ^ (by bit or ),~ (Bitwise inversion ).The bitwise inverse operator is a single object operator, and the rest are binary operators. The priority of bitwise operators ranges from high to low ~ , &, ^, |,Where ~ And the priority is higher than the arithmetic operator. The Union direction of other operators is from left to right, and the priority is lower than the relational operator. (1) bitwise AND operator (&)
Bitwise AND operation:
0 & 0 = 0, 0 & 1 = 0, 1 & 0 = 0, 1 & 1 = 1.
That is, the result is 1, and the result is 0.
For example, set the internal representation of 3
00000011
5 is represented
00000101
The result of 3 & 5 is
00000001
Bitwise AND operation have two typical usage: one is to take a few digits of a single bit string information. For example, the following code intercepts the minimum 7 digits of X: X & 0177. Second, let a variable retain a certain number of digits, and the remaining position is 0. For example, the following code keeps X at least 6 digits: x = x & 077. For the above usage, a constant must be designed first. Only the bit required for this constant is 1, and the bit not required is 0. It is used with the specified Bit String information by bit and. (2) bitwise OR operator (|)
Bitwise OR operations calculate the bitwise of the two calculation components according to the following rules:
0 | 0 = 0, 0 | 1 = 1, 1 | 0 = 1, 1 | 1 = 1
That is, as long as one digit is 1, the result is 1, otherwise it is 0.
For example, the result of 023 | 035 is 037.
A typical usage of bitwise OR operations is to divide the location of a single bit string into 1. For example, if you want to obtain the rightmost 4 as 1, the other bits are the same as those of the variable J. You can use the logic or operation 017 | j. To assign the result to variable J, you can write it:
J = 017 | j (3) bitwise XOR or operator (^)
Bitwise exclusive or operation calculates the bitwise of the two calculation components according to the following rules:
0 ^ 0 = 0, 0 ^ 1 = 1, 1 ^ 0 = 1, 1 ^ 1 = 0
That is, if the values of the corresponding bits are the same, the result is 0. If the values are different, the result is 1.
For example, the result of 013 ^ 035 is 026.
The XOR operation is used to determine whether the bitwise values of the two computation components are different. The difference is 1, and the same value is 0. A typical usage of bitwise exclusive or operation is to reverse the information of a single bit string. For example, if you want to reverse the information of the rightmost 4 bits of the integer variable J, you can use the logic XOR operation 017 ^ J to reverse the information of the rightmost 4 bits of J, that is, the first bits, the result is 0, and the original value is 0. The result is 1. (4) bitwise inverse operators (~)
Bitwise inversion is a single object operation. It is used to calculate the bitwise inversion of a single bit string, that is, the bitwise of which is 0, the result is 1, and the bitwise is 1, the result is 0. For example ,~ The result of 7 is 0xfff8.
Inverse operations are often used to generate constants irrelevant to system implementations. To set the minimum position of variable X to 0, the remaining digits remain unchanged, use the code X = x &~ 077. The above code is irrelevant to whether integer x is two bytes or four bytes.
When bitwise operations are performed on two data with different lengths (for example, long data and INT data), bitwise operations are performed on the right side of the two calculation components. If the number of short values is positive, 0 is used to fill up the full value; if the number of short values is negative, 1 is used to fill up the full value. If the short value is an unsigned integer, the high value is always filled with 0.
Bitwise operations are used to calculate the Bit String information to obtain the Bit String information result. For example, the following code can take down the integer variable K bit string information is 1 on the rightmost information bit :( (k-1) ^ K) & K. Shift operationShift operation is used Moving integer or numeric data as binary string. There are two operators:
<(Left shift) and> (right shift)
The shift operation is a binary operation that has two components: the left component is the object of the shifted data, and the right component value is the number of shifted digits. The shift operation regards the left component as a bit string information consisting of binary digits, and shifts it to the left or right to obtain the new bit string information.
The priority of the shift operators is lower than that of the arithmetic operators and higher than that of the Relational operators. Their union direction is from left to right. (1) Left shift operator (<)
The Left shift operation shifts the specified bit information of a single bit string to the left, and the empty bit in the right end is supplemented with 0. For example, 014 <2, and the result is 060, that is, 48.
When the left side is shifted, the left side of the left side is supplemented with 0, and the information of the left side is discarded. In binary operations, if the information is not lost due to the movement of information, moving one bit left is equivalent to multiplying 2. For example, 4 <2, the result is 16. (2) Right Shift Operator (>)
The right shift operation shifts the specified bit information of a single bit string to the right, and the bit information removed from the right is discarded. For example, 12> 2. The result is 3. In contrast to the Left shift, for a small integer, each right shift is 1, which is equal to dividing by 2. Pay attention to the symbol Bit During right shift. For the unsigned data, when the right shift is performed, the left-side blank space is supplemented with 0. For signed data, if the sign bit before the shift is 0 (positive), the left end is also supplemented with 0; if the sign bit before the shift is 1 (negative ), then the left end is supplemented with 0 or 1, depending on the computer system. For the right shift of negative numbers, the system supplemented with 0 is "logical right shift", and the system supplemented with 1 is "arithmetic right shift ". The following code illustrates the right-shift method used by the reader's computer system:
Printf ("% d \ n",-2> 4 );
If the output result is-1, it uses the arithmetic shift to the right; if the output result is a large integer, it is the logical shift to the right.
The shift operation and bit operation combine to implement many complex computations related to bit string operations. Set the right-to-left sequence number of the bit of the variable, from 0 to 15 digits. The expression for positioning is a positive integer not greater than 15. The following code has the meanings shown in the annotations on the right:
~ (~ 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 binary operations. In system software, binary bitwise is often required. The C language provides six bitwise operators. These operators can only be used for integer operands, that is, they can only be used for signed or unsigned char, short, Int, and long types.

C LanguageList of provided bitwise operators:
Operator description
& Bitwise AND if the two corresponding binary bits are both 1, The result value of this bit is 1, otherwise it is 0
| If one of the bitwise OR Two corresponding binary bits is 1, The result value of this bit is 1.
^ Bitwise OR 0 if the two binary values involved in the operation are the same; otherwise, 1
~ Reverse retrieval ~ It is a unary operator used to reverse a binary number by bit. It is about to change 0 to 1 and Change 1 to 0.
<Left shift is used to shift all the binary bits of a number to N places left, and 0 to the right
> Shift right: shifts the binary digits of a number N places to the right, and moves them to the low position on the right to be discarded. For the unsigned number, add 0 to the high position.


1. bitwise AND operator (&)
Bitwise AND refers to the two data involved in the operation, and the "and" operation is performed in binary bits. If both of the corresponding binary bits are 1, The result value of this bit is 1; otherwise, it is 0. Here, 1 can be interpreted as true in logic, and 0 can be interpreted as false in logic. The bitwise AND logic are consistent with the "and" operation rules. The logical "and" requires that the number of operations be true and the result is true. If a = true and B = true, then a bytes B = true. For example, the binary encoding of 3 & 5 3 is 11 (2 ). (In order to distinguish between decimal and other hexadecimal systems, this article stipulates that all non-decimal data is enclosed by brackets, which indicate the hexadecimal format in parentheses, And the binary value is marked as 2) the basic unit of memory data storage is byte. A byte is composed of eight bits. Bit is the minimum unit used to describe the data size of a computer. In a binary system, each 0 or 1 is a single bit. If 11 (2) is supplemented into one byte, It is 00000011 (2 ). The binary code of 5 is 101 (2). If it is supplemented into a byte, It is 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 );
}
Usage of bitwise AND:
(1) resetting
If you want to clear a storage unit, even if all its binary bits are 0, you only need to find a binary number, each of which meets the following conditions:

The original number is 1, and the corresponding digit in the new number is 0. Then perform the & Operation on the two to achieve the goal of clearing.
For example, if the original number is 43, that is, 00101011 (2), find another number and set it to 148, that is, 10010100 (2), bitwise and operation of the two:
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) locate certain indicators in a number
If you have an integer a (2 byte) and want to take the lower byte, you only need to bitwise A and 8 1.
A 00101100 10101100
B 00000000 11111111
C 00000000 10101100
(3) Retention refers to positioning:
Perform the bitwise AND operation with a number. This number is equal to 1.
For example, if you want to retain the numbers 84, I .e. 01010100 (2) from the left, the numbers 3, 4, 5, 7, and 8 are as follows:
01010100 (2)
& 00111011 (2)
00010000 (2)
That is, 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 (|)
If one of the two binary bits is 1, The result value of this bits is 1. In logic or operations, it is true.

.
For example, 60 (8) | 17 (8), bitwise OR operation is performed on October 60 and October 17.
00110000
| 00001111
00111111
C language source code:
# Include <stdio. h>
Main ()
{
Int A = 060;
Int B = 017;
Printf ("% d", a | B );
}
Application: bitwise OR operations are usually used to set a certain bit value of a data to 1. For example, if you want to change the low 4 bits of a number to 1, you only need to perform bitwise OR operations on a and 17 (8.


3. Exchange two values without temporary variables
For example, a = 3, that is, 11 (2); B = 4, that is, 100 (2 ).
To swap values of A and B, use the following value assignment statement:
A = A then B;
B = B then;
A = A then B;
A = 011 (2)
(Bytes) B = 100 (2)
A = 111 (2) (a has changed to 7)
(Bytes) B = 100 (2)
B = 011 (2) (B has a result, B has changed to 3)
(Bytes) A = 111 (2)


A = 100 (2) (a has changed to 4)
This is equivalent to the following two steps:
① Execute the first two assignment statements: "A = A then B;" and "B = B then a;" is equivalent to B = B then B ).
② Execute the third value assignment statement: A = A then B. Because the value of A is equal to (a then B), the value of B is equal to (B then a then B ),

Therefore, it is equivalent to a = A then B then a then B, that is, A is equal to a then B, equal to B.
Amazing!
C language source code:
# Include <stdio. h>
Main ()
{
Int A = 3;
Int B = 4;
A = a ^ B;
B = B ^;
A = a ^ B;
Printf ("A = % d B = % d", a, B );
}


4. "inverse" Operator (~)
It is a unary operator used to evaluate the binary anticode of an integer, that is, to change the value 1 of each binary bit of the operand to 0 to 1.
Example :~ 77 (8)
Source code:
# Include <stdio. h>
Main ()
{
Int A = 077;
Printf ("% d ",~ A );
}


5. Left shift operator (<)

The left-shift operator is used to remove multiple digits from the left of each binary number. The number of digits to be moved is specified by the right operand (the right operand must be non-negative ).

Value), and the right-side empty bits are filled with 0, and the high left overflow is discarded.
For example, if the binary number of A is shifted to two places, the empty bit on the right is supplemented with 0, and the overflow bit on the left is discarded. If a = 15, that is, 00001111 (2), Move 2 to the left.

The value is 00111100 (2 ).
Source code:
# Include <stdio. h>
Main ()
{
Int A = 15;
Printf ("% d", a <2 );
}
The value of one left shift is equal to the number multiplied by 2, and the value of two left shifts is equal to the number multiplied by 2*2 = <2 = 60, that is, 4. However, this conclusion only applies

When the number is shifted to the left, the overflow Discard high does not contain 1.
Assume that an integer is stored in one byte (8 bits). If expression A is an unsigned integer variable, the overflow value is 0 when expression A = 64 and expression a shifts one bit left.

When the Left shift is 2 bits, the overflow high contains 1.


6. Right Shift Operator (>)
The right-shift operator is used to shift the binary digits of a number to several places. The number of digits to be moved is specified by the right-hand operand (the right-hand operand must be non-negative.

Value), the low position to the right is discarded. For the unsigned number, the high position is supplemented by 0. For the signed number, some machines will leave the blank part on the left.

Fill with the symbol bit (that is, "arithmetic shift"), while other machines use 0 to fill the left blank part (that is, "logical shift "). Note

Meaning: For the unsigned number, move the upper position on the left to 0 when shifting the right. For the signed value, if the original signed bit is 0 (This number is positive), move the value on the left as well.

Enter 0. If the original symbol bit is 1 (negative), the left side is moved to 0 or 1, depending on the computer system used. Some systems move to 0, some

System migration 1. Moving 0 is called "logical shift", that is, simple shift; moving 1 is called "arithmetic shift ".
For example, the value of a is 113755 in octal:
A: 1001011111101101 (in binary format)
A> 1: 0100101111110110 (logical right shift time)
A> 1: 1100101111110110 (arithmetic shift to the right)
In some systems, a> 1 gets 045766 bits, while in other systems, 145766 is possible. Turbo C and some other C

The compilation uses the arithmetic right shift, that is, when the number of symbols is shifted to the right, if the symbol bit is originally 1, the left side is shifted to 1.
Source code:
# Include <stdio. h>
Main ()
{
Int A = 0113755;
Printf ("% d", a> 1 );
}


7. bitwise assignment operator

Bitwise operators and value assignment operators can form a composite value assignment operator.
Example: <=, |=, >>=, <=, Signature =
For example, a & = B is equivalent to a = A & B
A <= 2 is equivalent to a = A <2

To: c |, |, &, &, exclusive or ,~ ,! Operation

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.