...
/Advanced Arithmetic Operations on Integers
Advanced Arithmetic Operations on Integers
This lesson is a continuation of the previous lesson, and it explores some advanced arithmetic operations that we can perform with integers in D.
We'll cover the following...
We'll cover the following...
Arithmetic operations with assignment #
All of the operators that take two expressions have assignment counterparts. These operators assign the result back to the expression that is on the left-hand side:
Press + to interact
D
import std.stdio;void main() {int number = 10;number += 20; // same as number = number + 20; now 30writeln(number);number -= 5; // same as number = number - 5; now 25writeln(number);number *= 2; // same as number = number * 2; now 50writeln(number);number /= 3; // same as number = number / 3; now 16writeln(number);number %= 7; // same as number = number % 7; now 2writeln(number);number ^^= 6; // same as number = number ^^ 6; now 64writeln(number);}
Negation: - #
This operator converts the value of the expression from negative to positive or vice versa:
Press + to interact
D
import std.stdio;void main() {int number_1 = 1;int number_2 = -2;writeln(-number_1);writeln(-number_2);}
It is different from subtraction because it takes only one operand. Since unsigned types cannot have negative values, the result of using this operator with unsigned ...