[Knowledge Point] Operators in C + +

Source: Internet
Author: User

1. Preface

When I first learned grammar and basic knowledge, basically the simplest operators were in touch, and there was no concept at all about bit operations. Some of the operators that appear in C + +, especially the bit operators, are summarized today.

2, +-*/%

Doesn't that seem to bother you? All the pupils know.

3, && | |

The two basic logical operators,,&&, represent "and", connect two values before and after, shaped like a && B, and return a value of 1 if and only if a==1 and b==1, otherwise 0;| | means "or", connecting two values in front and back, shaped like a | | b, when a==1 or B==1, the return value is 1; otherwise 0.

4, << >>

Bitwise operator. form a << B, indicating that a moves the B bit to the left in the binary form. such as: << 2, [1010] (2) << 2, [101000] (2), 40. However, as long as you know the meaning of the binary number, and through this example, you can quickly find that the left one is equivalent to multiply 2, and the general use of more places is here, because the computer bit computing speed faster, so a=a*2 can be converted to a=a<<1, one to speed up, and second can be installed force Oh.

The same,<< is the opposite meaning, not much explanation.

5, & | ^

I think the 3rd item seems a bit like, but these are bitwise operators, and there are some similarities to the 3rd item. The common denominator of these bitwise operators is that they are converted to binary numbers, then bitwise computed, and finally the value is returned. To explain:

①& and

For each person, there is:

1&1=1;1&0=0;0&1=0;0&0=0.

That is, if and only if the two number of this bit is 1, the return value is 1, otherwise 0. Example: 10&3=[1010] (2) &[0011] (2) =[0010] (2) =2

As a whole, the Boolean return value returns 0 if and only if each bit returns a value of 0 o'clock, otherwise 1.

Application: When judging parity, it is usually written: if (n%2==0), can be written as: if (n&1==0).

②| Or

For each person, there is:

1|1=1;1|0=1;0|1=1;0|0=0.

That is, two digits of this bit as long as one is 1, the return value is 1; otherwise 0. Give an example: 10|3=[1010] (2) | [0011] (2) =[1011] (2) =11

As a whole, the Boolean return value returns 0 if and only if each bit returns a value of 0 o'clock, otherwise 1.

③^ XOR

For each person, there is:

1^1=0;1^0=0;0^1=0;0^0=1.

That is, the two number of this bit returns 1 at the same time, and does not return 0. Example: 10^3=[1010] (2) ^[0011] (2) =[0110] (2) =6

As a whole, the Boolean return value returns 0 if and only if each bit returns a value of 0 o'clock, otherwise 1.

Application: This is a very powerful bitwise operator, at least I think, because when I learned it all seemed impossible, but it turned out to be true.

<1> easier to exchange two-digit values

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

void swap (int &a,int &b) {a^=b; b^=a; a^=b;}

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

<2> Example: There is a number of 2*n+1 in a series, there are n number appeared two times, one count appeared once, please find out that number. First, it is easy to think of an algorithm, marking, time complexity of O (2n). However, skillfully using ^, can reach O (n), the following code:

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

void Find ()

{

int ans=0;

  for (int i=1;i<=n;i++) ans^=a[i];

printf ("%d", ans);

}

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

[Knowledge Point] Operators in C + +

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.