3.2-Applying Unary Operators Flashcards
What is a unary operator?
A unary operator is one that requires exactly one operand
What are the Unary Operators and its precedece?
- > Post-Increment/Decrement operators ++ –
- > Pre -Increment/Decrement operators ++ –
- > Unary plus operator +
- > Unary minus operator -
- > Bitwise complement operator ~
- > Logical complement operator !
- > Casting operator (type)
What does the Increment/Decrement operators, ++ and – do?
Respectively, can be applied to numeric operands and have a high order of precedence, as compared to binary operators.
🚀️ If the operator is placed 💥️BEFORE💥️ the operand, referred to as the pre-prefix operator.
🚀️ If the operator is placed 💥️AFTER💥️ the operand, referred to as the post-prefix operator.
🤯️⚠️📣️ What is the Post-prefix operator PITFALL case?
If the operand the post-prefix is being applied to is used in both sides of an assignment-expression.
-> The variable might NOT get incremented/decremented.
e.g: int x = 10, y = 10;
🕵♂️️ x = x++ + y; //20 ❌
🕵♂️️ x = x++ + y + x++; //31 ✅
What does the negation/minus operator, ‘-‘ do?
Reverses the sign of a numeric expression.
🚀️ By default every numeric expression goes with a plus operator
What does the logical complement operator (!) do?
Flips the value of a boolean expression.
What is the Unary bitwise complement symbol, what operation it performs?
bitwise complement ~ -> -(N +1) e.g: ~5 => -6