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