C language bit operations

Source: Internet
Author: User

The C language provides bitwise operations so that the C language can directly write system programs like assembly, and bit operations can effectively save memory in some scenarios with relatively strict memory; the C language provides the following bitwise operations: & bitwise AND if the two corresponding binary bits are both 1, The result value of this bit is 1, otherwise it is 0 | one of the two corresponding binary bits is 1, the result value of this bit is 1 ^ bitwise OR 0 if the two binary values involved in the operation are the same; otherwise, it is 1 ~ Bitwise inversion is used to reverse a binary number by bit, that is, 0 is changed to 1, and 1 is changed to 0 <left shift is used to shift all the binary bits of a number to N places left, right complement 0> right shift shifts the binary digits of a number to the right shift N places, and moves them to the right low position to be discarded. For the unsigned number, add 0 at the high position. For example: [cpp] # include <stdio. h> int main () {short a1 = 521; short a2 = 123; short And, Or, Not, AndOr; And = a1 & a2; Or = a1 | a2; not = ~ A1; AndOr = a1 ^ a2; printf ("a1, a2 bits And: % d \ n", And); printf ("a1, a2 bits or: % d \ n ", Or); printf (" a1, a2 bit: % d \ n ", Not); printf (" a1, a2 bit: % d \ n ", AndOr); return 0 ;}

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.