C-bit operation

Source: Internet
Author: User

C-bit operation
In many system programs, bit-level operations or processing are often required. The C language provides the bitwise operation function, which allows the C language to be used to write system programs like the assembly language.

The C language provides six bitwise operators:
& Bitwise AND

| By bit or

^ Bitwise OR

~ Invert

<Move left

> Right shift

12.1.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)

& Amp; 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 ).

[Example 12.1]

Main (){

Inta = 9, B = 5, c;

C = a & B;

Printf ("a = % dnb = % dnc = % dn", a, B, c );

}


12.1.2 bitwise OR operation
The 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.

For example, 9 | 5 can be written as follows:

00001001

| 00000101

00001101 (decimal 13) Visible 9 | 5 = 13

[Example 12.2]

Main (){

Inta = 9, B = 5, c;

C = a | B;

Printf ("a = % dnb = % dnc = % dn", a, B, c );

}


12.1.3 bitwise XOR operation
The bitwise XOR 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. For example, 9 ^ 5 can be written as follows:

00001001

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.