Dark Horse programmer--c Language learning experience--bit operator
-------Java training , Android training ,iOS training ,. Net Training , look forward to communicating with you! -------
The bitwise operator C language provides six bitwise operators:
& Bitwise AND
| Bitwise OR
^ Bitwise XOR OR
~ Take counter
<< left Shift
>> Right Shift
1. Bitwise AND Operation bitwise AND operator "&" are binocular operators. Its function is to participate in the operation of the two number of the corresponding binary phase. Only the corresponding two binaries are 1 o'clock, the result bit is 1, otherwise 0. The number of participating operations appears in a complementary fashion.
For example: The 9&5 can be written as follows: 00001001 (9 twos complement) &00000101 (5 's twos complement) 00000001 (1 of the twos complement) is visible 9&5=1.
Bitwise AND operations are usually used to clear some bits by 0 or to preserve certain bits. For example, A's high eight bits clear 0, reserved low eight bits, can be used as a&255 operation (255 binary number is 0000000011111111).
Application:
A. Clear 0 special positioning (mask in the specific position 0, the other bit is 1,s=s&mask)
B. Take a number of the middle finger positioning (mask in a specific position 1, the other bit is 0,s=s&mask)
2. Bitwise OR operation bitwise OR operator "|" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary phase or. As long as the corresponding two binary has one for 1 o'clock, the result bit is 1. The two numbers participating in the operation are in complement.
For example: 9|5 can be written as follows:
00001001|00000101
00001101 (decimal 13) Visible 9|5=13
Application:
It is commonly used to set the source operand to some location 1, other bits unchanged. (Mask in a specific position 1, the other bit is 0 s=s|mask)
3. Bitwise XOR or the bitwise XOR operator "^" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary dissimilarity or, when the two corresponding binary differences, the result is 1. Participation in the arithmetic is still in complement, for example 9^5 can be written as follows:
00001001^00000101 00001100 (decimal 12)
Application:
A. Reverse the value of a specific bit (at a specific position in mask 1, the other bit is 0 s=s^mask)
B. Do not introduce a third variable, exchange the value of two variables (set A=A1,B=B1)
Target Operation Post-operation status
A=A1^B1 a=a^b A=A1^B1,B=B1
B=A1^B1^B1 b=a^b A=A1^B1,B=A1
A=B1^A1^A1 a=a^b A=B1,B=A1
4. Negation operation negation Operator ~ is a monocular operator with right-associative. Its function is to reverse the bitwise negation of the number of participating operations. For example, the operation of the: ~ (0000000000001001) result is: 1111111111110110
5. Left shift operator "<<" is the binocular operator. Its function to the left of the "<<" operand of all the binary all left a number of bits, the "<<" to the right of the number to specify the number of bits moved, high drop, low 0. Its value is equal to multiply by 2. For example: A<<4 refers to moving the binary of a to the left by 4 bits. such as a=00000011 (decimal 3), 00110000 (decimal 48) After moving left 4 bits.
6. Right-shift operation right-shift operator ">>" is the binocular operator. The function is to shift all the binary of the left operand of ">>" to the right of several bits, and the number to the right of ">>" to specify the number of bits to move. Its value is equal to except 2.
For example: Set a=15,a>>2 to move 000001111 right to 00000011 (decimal 3). For the left-hand exit, if it is a positive number, the vacancy is 0, if it is negative, it may be 0 or 1, depending on the computer system used. Move in 0 called logical right SHIFT, move in 1 called arithmetic right shift, Turbo C with logical right shift.
Main () {
unsigned A, B;
printf ("Input a number:");
scanf ("%d", &a);
b=a>>5;
b=b&15;
printf ("a=%d b=%d", A, b);
}
-------Java training , Android training ,iOS training ,. Net Training , look forward to communicating with you! -------
Dark Horse programmer--c Language learning experience--bit operator