C Language Learning Tutorial eighth chapter-Enumeration, bitwise operations (3)

Source: Internet
Author: User
Tags bitwise bitwise operators printf

Bit operations

The various operations described earlier are performed in bytes as the most basic bit. However, in many system programs, it is often required to perform operations or processing at the bit level. C language provides the function of bitwise operation, which makes C language can be used as assembly language to write system program.
The bitwise operator C language provides six kinds of bitwise operators:
& Bitwise AND
| by bit or
^ per-bitwise XOR OR
~ Take the Counter
<< Move Left
>> Move Right

1. Bitwise AND Operation bitwise AND operator "&" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary phase. Only the corresponding two binary is 1 o'clock, the result bit is 1, otherwise 0. The number of participating operations appears in the complement method.
For example: 9&5 can be written as follows: 00001001 (9 of the binary complement) &00000101 (5 of the binary complement) 00000001 (1 of the binary complement) visible 9&5=1.

Bitwise AND operations are usually used to clear some bits to 0 or retain certain bits. For example, the height of a of eight-digit 0, retain a low eight-bit, can be used for a&255 operations (255 of the binary number of 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 OP 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 appear in complement.
For example: 9|5 can be written as follows: 00001001|00000101
00001101 (decimal is 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-XOR or operator "^" is the binocular operator. Its function is to participate in the operation of the two numbers of the corresponding binary dissimilarity or, when the two corresponding binary differences, the result is 1. Participation in the OP arithmetic still appears in complement, for example 9^5 can be written as follows: 00001001^00000101 00001100 (decimal 12)
Main () {
int a=9;
a=a^15;
printf ("a=%d\n", a);
}

4. The negation operator is calculated as a single operator with right binding. The function is to reverse the binary bitwise of the number participating in the operation. For example, ~9 's operation is: ~ (0000000000001001) The result is: 1111111111110110

5. Left shift operation Operator "<<" is the binocular operator. Its function shifts the operands of the operand on the left of "<<" to a number of digits, specifying the number of digits to move by the number to the right of "<<".
High drop, low 0. For example: A<<4 means to move the binary of a to the left by 4 digits. such as a=00000011 (decimal 3), 4-bit left, 00110000 (decimal 48). 6. Right shift operation Operator ">>" is the binocular operator. The function is to move the binary of the operands to the left of ">>" to the right several bits, and the number to the right of ">>" specifies the number of digits to move.
For example: Set A=15,a>>2 to move 000001111 to the right to 00000011 (decimal 3). It should be explained that for the signed number, the symbol bit will move along with the right shift. When a positive number, the highest bit of 0, while negative, the sign bit is 1, the highest bit is 0 or 1 depends on the compiler system requirements. Turbo C and many systems are defined as 1.
Main () {
unsigned a,b;
printf ("Input a number:");
scanf ("%d", &a);
b=a>>5;
b=b&15;
printf ("a=%d\tb=%d\n", a,b);
}
Please look at one more example!
Main () {
Char a= ' A ', b= ' B ';
int p,c,d;
P=a;
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.