Bit operation Comprehension

Source: Internet
Author: User
Tags bitwise operators

Bitwise operators the C language provides six bitwise operators:
& Bitwise AND
| By bit or
^ Bitwise OR
~ Invert
<Move left
> Right shift

1. bitwise and operation bitwise AND operator "&" are binary operators. 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 ).

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
# Include <iostream. h>
Void main (){
Int A = 9, B = 5, C;
C = A | B;
Cout <"A =" <A <Endl <"B =" <B <Endl <"c =" <C <Endl;
}

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)
# Include <iostream. h>
Void main (){
Int A = 9;
A = a ^ 15;
Cout <"A =" <A <Endl;
}

4. Search for Inverse operators ~ It is a single object operator and has the right combination. This function is used to reverse the binary numbers involved in the operation by bit. Example ~ 9 is calculated as follows :~ (0000000000001001) Result: 1111111111110110

5. The left shift operator <is a binary operator. Its function shifts the binary numbers on the left of "<" to several places left. The number on the right of "<" indicates the number of digits to be moved, Which is discarded at a high position and supplemented by 0 at a low position. For example, a <4 refers to moving each binary of a four places to the left. For example, if a = 00000011 (decimal 3), after four digits are left removed, the value is 00110000 (decimal 48 ).

6. the right shift operator ">" is a binary operator. Its function is to shift all the binary numbers on the left of ">" to several places right, and the right of ">" to the specified number of digits.
For example, if a = 15, A> 2, 000001111 is shifted to 00000011 (decimal 3 ). It should be noted that, for the number of signed characters, the symbol bit will be moved along with the right shift. When it is a positive number, the maximum bit is 0, while the negative number, the sign bit is 1, the maximum bit is 0 or fill 1 depends on the provisions of the compilation system. Turbo C and many systems require completing 1.
# Include <iostream. h>
Void main (){
Unsigned A, B;
Cout <"input a number ";
Cin>;
B = A> 5;
B = B & 15;
Cout <"A =" <A <"/TB =" <B <Endl;
}

Let's look at another example!
Main (){
Char A = 'A', B = 'B ';
Int P, c, d;
P =;
P = (P <8) | B;
D = P & 0xff;
C = (P & 0xff00)> 8;
Printf ("A = % d/Nb = % d/NC = % d/ND = % d/N", A, B, C, D );
}

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.