C language-bitwise operator, C language-Operator

Source: Internet
Author: User

C language-bitwise operator, C language-Operator
I. bitwise operators 1. bitwise AND :&

1> Functions

* The result bit is 1 only when the corresponding two binary bits are 1. Otherwise, the result bit is 0.

* Example: 10 is represented as 1010 in binary format and 7 is represented as 0111 in binary format. Calculate two values

* 1010

0111

--------------------

0010

* Rule: In the binary system, it is in the same position as the 1 phase, and the 0 phase is 0.

2> application: Use the & operator to determine the parity of an integer variable

1 int main () 2 {3 int a = 10; 4 a & 1? Printf ("% d is an odd number", a): printf ("% d is an even number", a); 5 return 0; 6}Use & to determine the parity of variables 2. bitwise OR: |

1> Functions

* If one of the two binary bits is 1, the result is 1. Otherwise, the result is 0.

2> Applications

* 10 is represented as 1010 in binary format, and 7 is represented as 0111 in binary format. Perform | operation on two values

* 1010

0111

--------------------

1111

3. bitwise OR: ^

1> Functions

* When the binary phase is different, the result is 1; otherwise, the result is 0.

* Example: 10 is represented as 1010 in binary format and 7 is represented as 111 in binary format. Perform the ^ operation on the two values

* 1010

0111

--------------------

1101

2> application: Use bitwise OR operators to swap two variable values

1 int main () 2 {3/* This usage is based on three rules. 4 1. perform an exclusive or operation for the same value, and the result is 0. 5 2. If any value excludes or with 0, the result is still the original value. 6 3. exchange Rules: a ^ B ^ c = a ^ c ^ B */7 int a = 10; 8 int B = 11; 9 a = a ^ B; // a = 10 ^ 1110 B = a ^ B; // B = 10 ^ 11 ^ 11 = 1011 a = a ^ B; // a = 10 ^ 11 ^ 10 = 1112 printf ("a = % d, B = % d", a, B); 13}Exchange Value

 

4. bitwise inversion :~

1> Functions

* Bitwise inversion is used to perform an inverse operation on each binary bit of a value. If the value is 0, the value is reversed to 1, and the value is reversed to 0.

2> Applications

* Example: 10 is represented as 1010 in binary format ~ Operation

* 1010

--------------------

0101

5. Shift left <, shift right>

1> Functions

* A <n indicates that each binary Bit Of a is moved to the left n bits, and the position is discarded at the top. The position is supplemented by 0 and the left n bits are actually multiplied by the n power of 2; a> n is to move the binary bits of a to the right n bits, to keep the symbol bit unchanged and to the right n bits. In fact, it is divided by the Npower of 2.

2> Applications

* Example: 10 is represented as 1010 in binary format, and 1010 is shifted to 1 bit. The result is 10100, that is, 20. 1010 is shifted to 1 bit. The result is 101, that is, 5.

* Note: When you perform multiplication or division on variables in the future, you can use shift left or shift right to improve the performance.

Related Article

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.