Computer Programming – Operators An operator in a programming language is a symbol that tells the compiler or interpreter to perform specific mathematical, relational, logical, bitwise, assignment or increment and decrement operation and produce final result. They are used in various computer languages including but not limited to Object Oriented and Procedural languages. The types of operators are described as follows: 1. Arithmetic Operators An arithmetic operator is a mathematical function that takes two operands and performs a calculation on them. The following table lists down a few of the important arithmetic operators available: Operator Description + Adds two operands - A + B will give 30 Subtracts second operand from the A - B will give - first 10 * Multiplies both operands / Divides numerator by de-numerator This gives remainder of an integer % Example division A * B will give 200 B / A will give 2 B % A will give 0 2. Relational Operators A relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. These include numerical equality (e.g., 5 = 5) and inequalities (e.g., 4 ≥ 3). They produce Boolean results which mean the result will be either true or false. The following table lists down a few of the important relational operators: Operator == Description Example Checks if the values of two operands are equal (A == B) is not or not, if yes then condition becomes true. true. 1 Operator Description Example Checks if the values of two operands are equal != or not, if values are not equal then condition (A != B) is true. becomes true. Checks if the value of left operand is greater than > the value of right operand, if yes then condition becomes true. (A > B) is not true. Checks if the value of left operand is less than < the value of right operand, if yes then condition (A < B) is true. becomes true. Checks if the value of left operand is greater than >= or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true. Checks if the value of left operand is less than or <= equal to the value of right operand, if yes then (A <= B) is true. condition becomes true. 3. Logical Operators Logical operators are very important in any programming language and they help us take decisions based on certain conditions. Suppose we want to combine the result of two conditions, then logical AND as well as OR logical operators help us in producing the final result. The following table shows all the logical operators: Operator Description Called Logical AND operator. If both the && operands are non-zero, then condition becomes true. Called Logical OR Operator. If any of the || two operands is non-zero, then condition becomes true. Example (A && B) is false. (A || B) is true. Called Logical NOT Operator. Use to ! reverses the logical state of its operand. If !(A && B) a condition is true then Logical NOT is true. operator will make false. 2 4. Bitwise Operators A bitwise operator is an operator used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits. Bitwise operators are used in: Communication stacks where the individual bits in the header attached to the data signify important information Embedded software for controlling different functions in the chip and indicating the status of hardware by manipulating the individual bits of hardware registers of embedded microcontrollers Low-level programming for applications such as device drivers, cryptographic software, video decoding software, memory allocators, compression software and graphics Maintaining large sets of integers efficiently in search and optimization problems Bitwise operations performed on bit flags, which can enable an instance of enumeration type to store any combination of values defined in an enumerator list Unlike common logical operators (like +, -, *), which work with bytes or groups of bytes, bitwise operators can check or set each of the individual bits within a byte. Bitwise operators never cause overflow because the result produced after the bitwise operation is within the range of possible values for the numeric type involved. The bitwise operators used in the C family of languages (C#, C and C++) are: OR (|): Result is true if any of the operands is true. AND (&): Result is true only if both operands are true. It can be used to set up a mask to check the values of certain bits. XOR (^): Result is true only if one of its operands is true. It is used mainly to toggle certain bits. It also helps to swap two variables without using a third one. Bitwise Complement or Inversion or NOT (~): Provides the bitwise complement of an operand by inverting its value such that all zeros are turned into ones and all ones are turned to zeros. >> (Right-Shift) and << (Left-Shift) Operator: Moves the bits the number of positions specified by the second operand in the right or left direction. While the right-shift operation is an arithmetic shift for operands of type int or long, it is a 3 logical shift for operands of type uint or ulong. Shift operators are used in aligning bits. By the way, there is also short and ushort and byte and sbyte. u means unsigned , so ulong is a large number without sign. You can store a bigger value in ulong than long, but no negative numbers allowed while ulong is also 64-bit, with all 64 bit to store the number. Operator Description & bitwise AND | bitwise OR ^ bitwise XOR << shift the left-hand operand left by the number of bits specified by the right-hand operand >> shift the left-hand operand right by the number of bits specified by the right-hand operand The binary & operator is used to clear bits from an integer operand. The binary | operator is used to set bits in an integer operand. The binary ^ operator returns one in each bit position where exactly one of the corresponding operand bits is set. The shift operators are used to move bits left or right in a given integer operand. Shifting left fills empty bit positions on the right-hand side of the result with zeroes. Shifting right using an unsigned integer operand fills empty bit positions on the left-hand side of the result with zeroes. Shifting right using a signed integer operand fills empty bit positions on the left-hand side with the value of the sign bit, also known as an arithmetic shift operation. Shifting an integer value by a negative number of bits or by a number of bits larger than the number of bits in the left-hand operand itself produces an undefined result. The compiler will produce an error message if the compiler can detect this condition when you compile your program. 4 In addition to the binary logical operators, the unary ~ operator may be used to perform a bitwise negation of a single operand: it converts each zero bit in the operand into a one bit, and each one bit in the operand into a zero bit. 5. Assignment Operators An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element in object oriented programming (OOP) languages such as Java, C# and so forth. Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands. 5.1. The following are the characteristics of assignment operators: When using the "=" operator for an assignment with the left operand as the property or indexer access, the property or indexer must have a set accessor. Overloading a binary operator implicitly overloads its corresponding assignment operator (if any). The different assignment operators are based on the type of operation performed between two operands such as addition (+=), subtraction, (-=), etc. The meaning of the operator symbol used depends on the type of the operands. Assignment operators are right-associative, which means they are grouped from right to left. Although assignment using assignment operator (a += b) achieves the same result as that without (=a +b), the difference between the two ways is that unlike in the latter example, "a" is evaluated only once. The assignment operator usually returns a reference to the object so as to be used in multiple assignments made in a single statement such as "a=b=c", where a, b and c are operands. The assignment operator expects the type of both the left- and right-hand side to be the same for successful assignment. In programming languages such as C#, an expression using an assignment operator might be "x op y", where x and y are operands and "op" represents the operator. The simple assignment operator "=" is used to store the value of its right-hand operand into the memory location denoted by the left-hand operand. The result is its return value. The other assignment operators that perform indicated operation on the two 5 operands and assign a resulting value to the left operand are called compound assignment operators. The following table shows all the aassignment operators: Operator Description = set the left-hand operand equal to the right-hand expression value += increment the left-hand operand by the right-hand expression value -= decrement the left-hand operand by the right-hand expression value *= multiply the left-hand operand by the right-hand expression value /= divide the left-hand operand by the right-hand expression value %= modulo the left-hand operand by the right-hand expression value |= bitwise OR the left-hand operand with the right-hand expression value &= bitwise AND the left-hand operand with the right-hand expression value ^= bitwise XOR the left-hand operand with the right-hand expression value <<= shift the left-hand operand left by the number of bits specified by the right-hand expression value >>= shift the left-hand operand right by the number of bits specified by the right-hand expression value 6. Increment and Decrement Operators In addition to the arithmetic assignment operators, Object Oriented Programming (OOP) also provides two unary operators for adding 1 to or subtracting 1 from the value of a numeric variable. These are the unary increment operator, ++, and the unary decrement operator, --, which are summarized in Fig. 6.1. A program can increment by 1 the value of a variable called c using the increment operator, ++, rather than the expression c=c+1 or c+=1. An increment or decrement operator that is prefixed to (placed before) a variable is referred to as the prefix increment or prefix decrement operator, respectively. An increment or decrement operator that is postfixed to (placed 6 after) a variable is referred to as the postfix increment or postfix decrement operator, respectively. Fig. 6.1 Increment and decrement operators Operator ++ Called preincrement Sample expression ++a Explanation Increment a by 1, then use the new value of a in the expression in which a resides. ++ postincrement a++ Use the current value of a in the expression in which a resides, then increment a by 1. -- predecrement --b Decrement b by 1, then use the new value of b in the expression in which b resides. -- postdecrement b-- Use the current value of b in the expression in which b resides, then decrement b by 1. Using the prefix increment (or decrement) operator to add (or subtract) 1 from a variable is known as preincrementing (or predecrementing) the variable. Preincrementing (or predecrementing) causes the variable to be incremented (decremented) by 1, then the new value of the variable is used in the expression in which it appears. Using the postfix increment (or decrement) operator to add (or subtract) 1 from a variable is known as post incrementing (or post decrementing) the variable. Post incrementing (or post decrementing) causes the current value of the variable to be used in the expression in which it appears, then the variable's value is incremented (decremented) by 1. ===================THE END================================= 7