Arduino> Structure> Compound Operators>
%=
[Compound Operators]
Description
This is a convenient shorthand to calculate the remainder when one integer is divided by another and assign it back to the variable the calculation was done on.
Syntax
x %= divisor; // equivalent to the expression x = x % divisor;
Parameters
x
: variable. Allowed data types: int.divisor
: non zero variable or constant. Allowed data types: int.Example Code
int x = 7;
x %= 5; // x now contains 2
Notes and Warnings
1. The compound remainder operator does not work on floats.
2. If the first operand is negative, the result is negative (or zero). Therefore, the result of
-------------------------------------------------------------------2. If the first operand is negative, the result is negative (or zero). Therefore, the result of
x %= 10
will not always be between 0 and 9 if x can be negative.&=
[Compound Operators]
Description
The compound bitwise AND operator
A review of the Bitwise AND & operator:
&=
is often 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.A review of the Bitwise AND & operator:
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 0 0 1 (operand1 & operand2) - returned result
Syntax
x &= y; // equivalent to x = x & y;
Parameters
x
: variable. Allowed data types: char, int, long.y
: variable or constant. Allowed data types: char, int, long.Example Code
Bits that are "bitwise ANDed" with 0 are cleared to 0 so, if myByte is a byte variable,
myByte & B00000000 = 0;
Bits that are "bitwise ANDed" with 1 are unchanged so,
myByte & B11111111 = myByte;
Notes and Warnings
Because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, B00000000 is shown for clarity, but zero in any number format is zero (hmmm something philosophical there?)
Consequently - to clear (set to zero) bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise AND operator (&=) with the constant B11111100
Consequently - to clear (set to zero) bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise AND operator (&=) with the constant B11111100
1 0 1 0 1 0 1 0 variable
1 1 1 1 1 1 0 0 mask
----------------------
1 0 1 0 1 0 0 0
bits unchanged
bits cleared
Here is the same representation with the variable’s bits replaced with the symbol x
x x x x x x x x variable
1 1 1 1 1 1 0 0 mask
----------------------
x x x x x x 0 0
bits unchanged
bits cleared
So if:
myByte = B10101010;
myByte &= B11111100; // results in B10101000
-------------------------------------------------------------------*=
[Compound Operators]
Description
This is a convenient shorthand to perform multiplication of a variable with another constant or variable.
Syntax
x *= y; // equivalent to the expression x = x * y;
Parameters
x
: variable. Allowed data types: int, float, double, byte, short, long.y
: variable or constant. Allowed data types: int, float, double, byte, short, long.Example Code
x = 2;
x *= 2; // x now contains 4
-------------------------------------------------------------------++
[Compound Operators]
Description
Increments the value of a variable by 1.
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
returns the new value of x
Parameters
x
: variable. Allowed data types: int, long (possibly unsigned).Returns
The original or newly incremented value of the variable.
Example Code
x = 2;
y = ++x; // x now contains 3, y contains 3
y = x++; // x contains 4, but y still contains 3
-------------------------------------------------------------------+=
[Compound Operators]
Description
This is a convenient shorthand to perform addition on a variable with another constant or variable.
Syntax
x += y; // equivalent to the expression x = x + y;
Parameters
x
: variable. Allowed data types: int, float, double, byte, short, long.y
: variable or constant. Allowed data types: int, float, double, byte, short, long.Example Code
x = 2;
x += 4; // x now contains 6
-------------------------------------------------------------------—
[Compound Operators]
Description
Decrements the value of a variable by 1.
Syntax
x--; // decrement x by one and returns the old value of x
--x; // decrement x by one and returns the new value of x
Parameters
x
: variable. Allowed data types: int, long (possibly unsigned).Returns
The original or newly decremented value of the variable.
Example Code
x = 2;
y = --x; // x now contains 1, y contains 1
y = x--; // x contains 0, but y still contains 1
--------------------------------------------------------------------=
[Compound Operators]
Description
This is a convenient shorthand to perform subtraction of a constant or a variable from a variable.
Syntax
x -= y; // equivalent to the expression x = x - y;
Parameters
x
: variable. Allowed data types: int, float, double, byte, short, long.y
: variable or constant. Allowed data types: int, float, double, byte, short, long.Example Code
x = 20;
x -= 2; // x now contains 18
-------------------------------------------------------------------/=
[Compound Operators]
Description
This is a convenient shorthand to perform division of a variable with another constant or variable.
Syntax
x /= y; // equivalent to the expression x = x / y;
Parameters
x
: variable. Allowed data types: int, float, double, byte, short, long.y
: non zero variable or constant. Allowed data types: int, float, double, byte, short, long.Example Code
x = 2;
x /= 2; // x now contains 1
-------------------------------------------------------------------^=
[Compound Operators]
Description
The compound bitwise XOR operator ^= is often used with a variable and a constant to toggle (invert) particular bits in a variable.
A review of the Bitwise XOR ^ operator:
A review of the Bitwise XOR ^ operator:
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 1 1 0 (operand1 ^ operand2) - returned result
Syntax
x ^= y; // equivalent to x = x ^ y;
Parameters
x
: variable. Allowed data types: char, int, long.y
: variable or constant. Allowed data types: char, int, long.Example Code
Bits that are "bitwise XORed" with 0 are left unchanged. So if myByte is a byte variable,
myByte ^ B00000000 = myByte;
Bits that are "bitwise XORed" with 1 are toggled so:
myByte ^ B11111111 = ~myByte;
Notes and Warnings
Because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, B00000000 is shown for clarity, but zero in any number format is zero.
Consequently - to toggle bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise XOR operator (^=) with the constant B00000011
Consequently - to toggle bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise XOR operator (^=) with the constant B00000011
1 0 1 0 1 0 1 0 variable
0 0 0 0 0 0 1 1 mask
----------------------
1 0 1 0 1 0 0 1
bits unchanged
bits toggled
Here is the same representation with the variables bits replaced with the symbol x. ~x represents the complement of x.
x x x x x x x x variable
0 0 0 0 0 0 1 1 mask
----------------------
x x x x x x ~x ~x
bits unchanged
bits set
So if:
myByte = B10101010;
myByte ^= B00000011 == B10101001;
-------------------------------------------------------------------|=
[Compound Operators]
Description
The compound bitwise OR operator |= is often used with a variable and a constant to "set" (set to 1) particular bits in a variable.
A review of the Bitwise OR | operator:
A review of the Bitwise OR | operator:
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 1 1 1 (operand1 | operand2) - returned result
Syntax
x |= y; // equivalent to x = x | y;
Parameters
x
: variable. Allowed data types: char, int, long.y
: variable or constant. Allowed data types: char, int, long.Example Code
Bits that are "bitwise ORed" with 0 are unchanged, so if myByte is a byte variable,
myByte | B00000000 = myByte;
Bits that are "bitwise ORed" with 1 are set to 1 so:
myByte | B11111111 = B11111111;
Notes and Warnings
Because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, B00000000 is shown for clarity, but zero in any number format is zero.
Consequently - to set bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise OR operator (|=) with the constant B00000011
Consequently - to set bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise OR operator (|=) with the constant B00000011
1 0 1 0 1 0 1 0 variable
0 0 0 0 0 0 1 1 mask
----------------------
1 0 1 0 1 0 1 1
bits unchanged
bits set
Here is the same representation with the variables bits replaced with the symbol x
x x x x x x x x variable
0 0 0 0 0 0 1 1 mask
----------------------
x x x x x x 1 1
bits unchanged
bits set
So if:
myByte = B10101010;
myByte |= B00000011 == B10101011;
-------------------------------------------------------------------
Post a Comment
Hi Users, if you have any queries then please let me know.