Introduction to Windows Operators
5 9 43 1 true false. These random numbers and text don’t make any sense, do they? No, they don’t. That’s because they lack operators.
Any meaningful expression is a combination of variables and operators. An operator determines how the variables are connected through each other and how they would contribute to the end result.
5 + 9 – 43 < 1 ? true: false. Now it makes some sense. So let’s snorkel through the world of operators in Windows.
Classification of Windows Operators
These Windows Operators are broadly classified into three types. This classification is done based on the number of variables or operands an operator requires. The three types are:
- Unary Operators
- Binary Operators
- Ternary Operators
1. Unary Operators: They require a single operand.
E.g. Prefix and Postfix operators, Shorthand operators, Negation Operator etc
2. Binary Operators: They require two operands to calculate the result.
E.g. Arithmetic operators, logical operators etc.
3. Ternary Operators: They require three operands.
E.g. Ternary Conditional operator
Types of Windows Operators
The various types of windows operators, based on their functionality are:
1. Basic Arithmetic Operators
These windows operators perform mathematical calculations.
Plus operator (+): Adds or concatenates the two operands.
- Sum of two integers: 1+3 results in 4
- Sum of two floating point numbers: 9.8+0.4 results in 10.2
- Concatenation of two strings: “Hello”+”World” results in “Hello World”
Minus Operator (-): Subtracts the second operand from first. Doesn’t work on strings.
- Subtraction of two integers: 5-4 results in 1
- Subtraction of two floating point numbers: 4.1 – 4.6 results in -0.5
Multiplication Operator (*): Multiplies the two operands.
- Multiplication of two integers: 9*5 results in 45
- Multiplication of two floating point numbers: 1.1*2.3 results in 2.53
Division Operator (/): Divides the first operand by the second and returns the quotient as the result. The remainder is discarded. Some advanced languages, however, do not discard the remainder and keep dividing until a pre-set number of precision points is reached.
- Division of two integers: 45/11 results in 4
- In advanced languages: 45/11 results in 4.090909
Modulus Operator (%): Divides the first operand by the second and returns the remainder as the result. The quotient is discarded. Doesn’t work on floating point numbers.
- Modulus of two integers: 45/11 results in 1
2. Assignment Operator (=)
Assigns the result calculated in the right-hand side of the operator (RHS) to the left-hand variable (LHS). The left of the operator should always be a variable and not a constant/expression.
- x = 5, assigns value 5 to x.
- 5 = x is invalid as left-hand side is a constant.
- y = x*4 calculates x*4 and assigns to y. Thus, y now holds the value 20
- x*4 = y is invalid as the left-hand side is an expression.
3. Comparison Operators
They compare the value of the first operand with that of the second operand and returns either true or false. These are less than (<), greater than (>), less than or equal (<=), greater than or equal (>=), equal (==), not equal (!=).
- 61>45, returns true.
- 3==3, returns true.
4. Prefix and Postfix Operators
These windows operators increment or decrement the value of an operand by 1. They work only on integers.
x++, x is now 6
–x, x is now 5 again
Seems simple, right? There is a very significant difference in the functioning of the two operators. Prefix operators change the value of the operand before evaluating the expression, whereas the postfix operator changes the value after the expression has been evaluated.
- x = 5
print(x++), this will print 5 and then change the value of x to 6
print(++x), this will increment the value from 6 to 7 and then print 7.
5. Shorthand Operators
These windows operators are a combination of two operators. The result is calculated using the existing value of the operand and assigned back to itself. They help minimize the lines of code written. The most common shorthand operators are:
- +=: This is equivalent to addition and assignment.
- -=: This is equivalent to subtraction and assignment.
- *=: This is equivalent to multiplication and assignment.
- /=: This is equivalent to division and assignment.
E.g. – x+=5, is equivalent to x=x+5.
6. Logical Operators
Logical operators are mainly used to control the program flow. Usually, they help the compiler on which path to follow based on the outcome of a decision. They always result in boolean values
Logical AND (&&): Returns true if the conditions on both left and right side of the operator are true, otherwise returns false.
- (2>3) && (4<5) returns false. Reason, 2 is not greater than 3
- Boolean b1 = true
Boolean b2 = true
b1 && b2 returns true.
Logical OR (||): Returns true if any of the operands is true, otherwise returns false.
- (2>3) || (4<5) returns true
- Boolean b1 = false
Boolean b2 = false
b1 || b2 returns false.
Logical NOT / Negation (!): Inverses the result of the operand i.e. true becomes false and false becomes true.
- ! (2>3) returns true
- ! (2>3) && (4<5) returns true. Reason -! (2>3) results in true.
7. Bitwise Operators
Bitwise operators are a special category of operators as they do not operate in a conventional way. While all other operators operate on bytes, bitwise operators operate on bits. Don’t panic. They may sound tough but are easy to understand through examples.
Let us assume we have two numbers 2 and 4. Their respective binary conversions would be 0010 and 0100. Since 1 byte contains 8 bits, we convert them to 0000 0010 and 0000 0010.
- Bitwise AND (&): 2 & 4 results in 0000 0000 which is simply 0
- Bitwise OR (|): 2 | 4 results in 0000 0110 which is 6
- Bitwise NOT (~): ~2 results in 1111 1101 which is -125 most significant bit are the sign bit
Note: Bitwise operators are a vast topic in themselves and they play a key role in the communication industry. It is recommended to deep dive in bitwise operators for a better understanding.
8. Ternary Operator
The ternary operator is a shorthand operator for a logical if and else program flow. It evaluates the expression on the left of the question mark (?) and based on the result (true/false) the operations on the left and right of the colon (:) are performed.
E.g. – (condition)? (operation if true): (operation if false)
- (5>9) ? (print true): (print false) false is printed.
9. Operator Precedence
The precedence of operators is as follows (highest to lowest priority):
- Prefix and Postfix operators
- Multiplication, Division, Modulus
- Addition, Subtraction
- Bitwise Operators
- Logical Operators (some logical operators take higher precedence than bitwise operators. Learn more when you deep dive in bitwise operator section.)
- Ternary Operator
- Assignment, Shorthand operators
This has been a guide to Windows Operator. Here we have discussed the different type of windows operators with examples. You can also go through our other suggested articles to learn more –