C + + bit operations (binary)

Source: Internet
Author: User

C + + bit operations

--binary calculation (by operation Priority)

Foreword: We all know that all the operations of a computer are actually performed in binary, so the speed of bit operations in binary conditions is actually very fast, and the time complexity of the program can be reduced by using proper bit arithmetic in programming.

~ Take the counter: ~x, said the X of you to reverse, that is, ~1=0,~1=0.

>> shift Right: x>>p, which means to move X to the right P-bit, which can be understood as the number x right-hand side of the binary. Example: (2) 10>>1= (1) 2>> 1 = = "(11) 2= (1); () 10>>2= (1111) 2>>2 = =" (2) 3 = (10)

<< left: x<<p, which means to shift x to the left P-bit, which can be understood as the number of binary x on the right side of P 0. Example: (1) 10<<1= (1) 2<<1 = = "(Ten) 2= (2), (3) 10<<3= (one) 2<<3 =" (11000) 2 = (24) 10

& Bitwise AND: X&p, which means that x and p are performed and operated in binary, and the law is: 1&1=1, 1&0=0, each of the 0&0=0,x and each of the P is computed according to this rule.

Example: 2&3 ==> 10, which results in 2

&

10

^ Bitwise XOR: X^p, which indicates that X and P perform an XOR operation in binary, the law is: each of the 1^1=0,1^0=1,0^0=0,x and p each is computed according to this rule.

Example: 2&4 = = "010, which results in 6

^

110

| bitwise OR: X|P, which means that X and p are carried out or operated in binary, the law is: each of the 1|1=1,1|0=1,0|0=0,x and P is calculated as the result of this rule.

Example: 2|4== "010, which results in 6

|

110

C + + bit operations (binary)

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.