C + + Operators

Source: Internet
Author: User

1. Arithmetic operators (+-/*%)

2. Shift Operators

Shift operator: The operand must be shaped, >>, and the left-hand shift of the logical left is filled with 0, and the bit with the left-hand shift left is padded with the sign bit. (The unsigned number is the logical left shift, for the signed number depends on the editor)

<<, the number left of the value is discarded, the right side of the more than a few vacant seats with 0.

Example: Returns the number of 1 in a parameter

int sumofone (int num) {int count = 0;         for (int i = 0; i <; i++) {
if (num &1) count++; num >>= 1;} return count;}

3, Bitwise operators (& | ^

Place the specified bit (bit_number) 1

value=value|1<<bit_number;

Place the specified bit (bit_number) 1

value=value&~ (1<<bit_number)

4, assignment, compound assignment operator (=,+=,-=,*=,%=, ...) )

5. Monocular operator (only one operand is accepted) (!,++,-,&,sizeof,~,--, *, (type))

6. Relational operators (>,>=,<,<=,!=,==)

7. Logical Operators (&&,| | )

A>5&&A<10 (&& has a lower priority than > and <, so the combination is (a>5) && (a<10) But despite the && has a lower priority but it also controls two expressions to manipulate the right operand only if the left operand is true

Some applications of the operator:

Determine if a number is 2 of the n-th square

BOOL Ispowoftwo (int  num) {    if1))        return1;     return0;}

Defines a macro implementation that swaps the odd and even bits of a number in an int type

#define M (N) (((n>>1) 0x55555555) | (n<<1) &0XAAAAAAAA)

Turn a binary sequence in reverse order

int Reverse (int  num) {    int0;      for (int0; i++)    {        1;        RET1;    }     return ret;}

Find the only occurrence of a number in a group

int Findonecount (intint  size) {    int0;      for (int0; i < size; i++)    {        = xor^a[i];    }     return xor;}

Find the two numbers that only appear once, and the others appear in pairs.

voidFindtwocount (int*a,intSizeint*P1,int*p2) {    intTMP =0; intXOR =0; intpos =0;  for(inti =0; i < size; i++) {XOR^=A[i]; } tmp=xor;  while(XOR)//Find a different one {if(XOR &1) ==1)             Break; POS++; XOR>>=1; }     for(inti =0; i < size; i++)    {        if((A[i] >> POS) &1)        {            (*P1) ^=A[i]; } XOR=tmp; *P2 = xor ^ (*p1); } printf ("%d\t%d\n", *P1, *p2);}

C + + Operators

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.