Dart, a versatile and modern programming language developed by Google, incorporates a variety of operators that are important in manipulating and evaluating values. Among these, unary operators are powerful tools that allow developers to perform operations on a single operand. In this article, we’ll explore Dart’s unary operators, both unary postfix and unary prefix operators and providing comprehensive code examples to solidify your understanding.
Introduction to Unary Operators
Unary operators are those that operate on a single operand. Dart supports both unary postfix and unary prefix operators, each serving distinct purposes in programming. Let’s explore them in detail.
Postfix Unary Operators
Postfix unary operators are applied after the operand. Dart supports two postfix unary operators: operand++ (post-increment) and operand– (post-decrement).
Postfix Increment (operand++)
The unary postfix increment operator increments the value of the operand after the current value is used in an expression. Let’s illustrate this with a simple example:
void main() {
int x = 5;
int result = x++;
print('Result: $result'); // Output: Result: 5
print('Updated x: $x'); // Output: Updated x: 6
}
In this example, x++ increments the value of x after assigning its current value to the variable result.
Postfix Decrement (operand–)
Similarly, the unary postfix decrement operator decrements the value of the operand after its current value is used in an expression. Let’s explore this through an example:
void main() {
int y = 8;
int result = y--;
print('Result: $result'); // Output: Result: 8
print('Updated y: $y'); // Output: Updated y: 7
}
Here, y– decrements the value of y after assigning its current value to the variable result.
Prefix Unary Operators
Prefix unary operators, on the other hand, are applied before the operand. Dart supports a variety of prefix unary operators, each serving a unique purpose: -operand, !operand, ~operand, ++operand, and –operand.
Negation (-operand)
The unary prefix operator negates the value of the operand. It is commonly used to change the sign of a numeric value, negative to positive, and vice versa. Let’s see this in action:
void main() {
int a = 10;
int negatedValue = -a;
print('Original value: $a'); // Output: Original value: 10
print('Negated value: $negatedValue'); // Output: Negated value: -10
}
In this example, the -a expression negates the value of a.
Logical NOT (!operand)
The unary prefix operator !operand is the logical NOT operator. It negates the truth value of the operand. It reverses the logical value of operand, true to false and vice versa. This is used in Boolean expressions. Let’s explore this with a boolean variable:
void main() {
bool isTrue = true;
bool isFalse = !isTrue;
print('Original value: $isTrue'); // Output: Original value: true
print('Negated value: $isFalse'); // Output: Negated value: false
}
Here, !isTrue negates the truth value of the boolean variable isTrue.
Bitwise NOT (~operand)
The unary prefix operator ~operand is the bitwise NOT operator. It flips the bits of the operand, changing 0s to 1s and vice versa. This is used in bitwise operations. However, the result may not be immediately intuitive, especially when dealing with signed integers. Let’s examine a bitwise NOT example:
void main() {
int num = 5;
print("Original value in binary: ${num.toRadixString(2)}"); // Output: Original value in binary: 101
int result = ~num;
print("Result after bitwise negation: ${result.toRadixString(2)}"); // Output: Result after bitwise negation: -110
}
Here, ~num performs a bitwise NOT operation on the integer num.
Two’s Complement Representation
The seemingly unexpected result of -110 is a consequence of Dart’s use of two’s complement representation for signed integers. In two’s complement, the leftmost bit is the sign bit, where 0 represents a positive number and 1 represents a negative number.
Prefix Increment (++operand)
The unary prefix operator ++operand is the pre-increment operator. It increments the value of the operand before its current value is used in an expression. Let’s see how this works:
void main() {
int p = 3;
int updatedValue = ++p;
print('Updated value: $updatedValue'); // Output: Updated value: 4
print('Updated p: $p'); // Output: Updated p: 4
}
In this example, ++p increments the value of p before assigning it to the variable updatedValue.
Prefix Decrement (–operand)
Finally, the unary prefix operator –operand is the pre-decrement operator. It decrements the value of the operand before its current value is used in an expression. Let’s explore this through an example:
void main() {
int q = 7;
int updatedValue = --q;
print('Updated value: $updatedValue'); // Output: Updated value: 6
print('Updated q: $q'); // Output: Updated q: 6
}
Here, –q decrements the value of q before assigning it to the variable updatedValue.
Conclusion
Understanding and utilizing unary operators is essential for writing efficient and concise Dart code. In this article, we explored both postfix and prefix unary operators, including increment, decrement, negation, logical NOT, and bitwise NOT. Unary operators can enhance the readability and expressiveness of your programs while allowing you to perform complex operations with ease.
References: