Computer Programming Operators Reading
Computer Programming Operators Reading
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:
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:
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
(A > B) is not
> than the value of right operand, if yes then
true.
condition becomes 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
(A >= B) is not
>= than or equal to the value of right operand, if yes
true.
then condition becomes 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:
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
logical shift for operands of type uint or ulong. Shift operators are used in aligning
bits.
3
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 OR
^ bitwise XOR
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.
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.
4
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 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
5
= set the left-hand operand equal to the right-hand expression value
&= bitwise AND 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
Operator Called Sample Explanation
expression
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.
N.B Please take note of the Arithmetic operators and the rules of precedence.
➔ Kindly be aware that when operators with the same precedence occur in one expression, they
will be executed from left to right.
a) Step 1: Parentheses (or brackets) have higher priority than any other of the operators.
E.g. ()
b) Step 2: Exponentiation has the second highest order of precedence. E.g. ^
7
c) Step 3: Multiplication and division are done from left to right and have third highest order
of precedence. E.g. * and /
d) Step 4: Integer division is done next. E.g. \
• Integer division: - can only be done when dividing an integer value by another integer
value because it discards (drops) the decimal part and doesn’t round the answer.
Example: 39 \ 10 = 3
• Modulus arithmetic: - is done when the user only wants to know what the value of the
remainder is when dividing. Example: 39 mod 10 = 9
e) Step 5: Modulus arithmetic is done before addition or subtraction. E.g. mod
f) Step 6: Finally, addition and subtraction are done from left to right, so subtraction is
done first because the minus is to the left of the plus. E.g. – and +
=================================THE END================================