Arduino - Compound Operators


Advertisements

Assume variable A holds 10 and variable B holds 20 then −

Operator name Operator simple Description Example
increment ++ Increment operator, increases integer value by one A++ will give 11
decrement -- Decrement operator, decreases integer value by one A-- will give 9
compound addition += Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand B += A is equivalent to B = B+ A
compound subtraction -= Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand B -= A is equivalent to B = B - A
compound multiplication *= Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand B*= A is equivalent to B = B* A
compound division /= Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand B /= A is equivalent to B = B / A
compound modulo %= Modulus AND assignment operator. It takes modulus using two operands and assign the result to left operand B %= A is equivalent to B = B % A
compound bitwise or |= bitwise inclusive OR and assignment operator A |= 2 is same as A = A | 2
compound bitwise and &= Bitwise AND assignment operator A &= 2 is same as A = A & 2

Example

void loop () {
   int a = 10,b = 20
   int c = 0;
   
   a++;
   a--;
   b += a;
   b -= a;
   b *= a;
   b /= a;
   a %= b;
   a |= b;
   a &= b;
}

Result

a = 11
a = 9
b = 30
b = 10
b = 200
b = 2
a = 0
a = 0
a = 30
arduino_operators.htm
Advertisements