Structure: Compound Operators Flashcards
Increment and Decrement
++ & –
Syntax
x++; // increment x by one and returns the old value of x
++x; // increment x by one and returns the new value of x
x– ; // decrement x by one and returns the old value of x
–x ; // decrement x by one and returns the new value of x
Examples
x = 2; y = ++x; // x now contains 3, y contains 3 y = x--; // x contains 2 again, y still contains 3
Allows you to perform a mathematical operation through a shorthanded way.
+=, -=, *=, /=, %=
Syntax
x += y; // equivalent to the expression x = x + y;
x -= y; // equivalent to the expression x = x - y;
x *= y; // equivalent to the expression x = x * y;
x /= y; // equivalent to the expression x = x / y;
x %= y; // equivalent to the expression x = x % y;
Examples
x = 2;
x += 4; // x now contains 6
x -= 3; // x now contains 3
x *= 10; // x now contains 30
x /= 2; // x now contains 15
x %= 5; // x now contains 0
used with a variable and a constant to force particular bits in a variable to the LOW state (to 0). This is often referred to in programming guides as “clearing” or “resetting” bits.
Used with variable often to set to one.
x &=, x |=
Syntax Compound &:
x &= y; // equivalent to x = x & y;
Parameters
A character, int or long variable
Example: Look online
Syntax Compound or
x |= y; // equivalent to x = x | y;
Example:
0 0 1 1 operand1
0 1 0 1 operand2
0 1 1 1 (operand1 | operand2) - returned result