2 Operators
2 Operators
2 Operators
AIRTHMETIC OPERATORS
+, - , * , / , %, ++, --
The precedence of operators determines the order in which operators are evaluated within an
expression. Operator precedence refers to the rules that determine the order in which
operators are evaluated in an expression. When an expression contains multiple operators, the
precedence rules help dictate which operators are evaluated first and which are evaluated
later. This ensures that expressions are evaluated in a logical and consistent manner.
Parentheses ((), [ ], { }): Expressions within parentheses are evaluated first. Nested
parentheses are evaluated from the innermost to the outermost.
Unary Operators (+, -, ++, --, !, ~, * [pointer dereference], & [address-of]): Unary
operators are applied to a single operand. They have higher precedence than binary
operators.
Multiplicative Operators (*, /, %): These operators perform multiplication, division, and
modulo operations. They are evaluated from left to right.
Additive Operators (+, -): These operators perform addition and subtraction. They are
evaluated from left to right.
ASSIGMENT STATEMENT
A= B+C x=y=100
+= , -=, etc
Operand to the left side of = are lvalue and operand to the right side of = is rvalue
X=5; is correct 5=x is incorrect
INCREMENT OPERATOR
It is a unary operator used to increase the value of variable by 1.Increment operator cannot
increment the value of constant and expressions.
DECREMENT OPERATOR
Prefix and postfix operators are used in programming languages, including C++, to modify the
value of variables or expressions. They differ in the timing of the operation and the value that is
returned. Here's a comparison of prefix and postfix operators:
SIZE OF OPERATOR
The sizeof operator in C++ is used to determine the size, in bytes, of a data type or an
expression. It provides information about the memory space occupied by a data type or object
in memory. The syntax of the sizeof operator is as follows:
sizeof(operand)
sizeof(int);
LOGICAL OPERATORS
Logical operators in programming, including C++, are used to perform logical operations on
Boolean values (`true` or `false`). They allow you to combine or manipulate Boolean values to
make decisions, control program flow, and create complex conditions. C++ provides three main
logical operators: AND (`&&`), OR (`||`), and NOT (`!`). Here's an overview of each logical
operator:
- The logical AND operator (`&&`) returns `true` if both of its operands are `true`, otherwise, it
returns `false`.
- It evaluates the second operand only if the first operand is `true`. If the first operand is
`false`, the second operand is not evaluated, as the result of the expression is already known to
be `false`.
- Example: `if (x > 0 && y < 10) { /* code */ }`
2. **Logical OR (`||`):**
- The logical OR operator (`||`) returns `true` if at least one of its operands is `true`,
otherwise, it returns `false`.
- It evaluates the second operand only if the first operand is `false`. If the first operand is
`true`, the second operand is not evaluated, as the result of the expression is already known to
be `true`.
- The logical NOT operator (`!`) reverses the logical value of its operand. If the operand is
`true`, `!` turns it into `false`, and if the operand is `false`, `!` turns it into `true`.
Logical operators are frequently used in conditional statements (`if`, `while`, `for`, etc.) and in
creating more complex conditions. They help you build decision-making structures and control
the flow of your program based on different combinations of conditions.
RELATIONAL OPERATORS
Relational operators in programming, including C++, are used to compare two values and
determine the relationship between them. They return a Boolean value (`true` or `false`) based
on the comparison result. Relational operators are fundamental for making decisions and
controlling program flow based on conditions. C++ provides the following relational operators:
1. **Equal to (`==`):**
- The equal to operator (`==`) compares two values to check if they are equal.
- The not equal to operator (`!=`) compares two values to check if they are not equal.
- Returns `true` if the values are not equal, and `false` if they are equal.
- The greater than operator (`>`) compares two values to check if the left value is greater than
the right value.
- The less than operator (`<`) compares two values to check if the left value is less than the
right value.
- The greater than or equal to operator (`>=`) compares two values to check if the left value is
greater than or equal to the right value.
- Returns `true` if the left value is greater than or equal, and `false` otherwise.
- The less than or equal to operator (`<=`) compares two values to check if the left value is less
than or equal to the right value.
- Returns `true` if the left value is less than or equal, and `false` otherwise.
Relational operators are used extensively in conditional statements, loops, and other situations
where you need to compare values and make decisions based on their relationships.
BITWISE OPERATOR
It works on bits and perform bit by bit operations. Bitwise operators in programming, including
C++, are used to perform operations on individual bits of integers or other binary data. These
operators work at the binary level, manipulating the individual bits of numbers. Bitwise
operations are often used for low-level tasks like optimizing code, manipulating hardware
registers, and performing tasks related to data compression and encryption.
Performs a bitwise AND operation between the corresponding bits of two operands. If both bits
are 1, the result is 1. Otherwise, the result is 0.
Bitwise OR (|):
Performs a bitwise OR operation between the corresponding bits of two operands. If either bit
is 1, the result is 1. If both bits are 0, the result is 0.
Performs a bitwise exclusive OR (XOR) operation between the corresponding bits of two
operands.If the bits are different (one is 0 and the other is 1), the result is 1. If the bits are the
same, the result is 0.
Performs a bitwise NOT operation on a single operand.It inverts each bit of the operand. If the
bit is 0, it becomes 1, and if the bit is 1, it becomes 0.
A= 0011 0011 ~A = 1100 1100
Shifts the bits of the left operand to the left by a specified number of positions. The vacated
positions are filled with zeros.
A= 0011 0011
A<<2
Shifts the bits of the left operand to the right by a specified number of positions. Depending on
the data type, the vacated positions might be filled with zeros or the sign bit.
A= 0011 0011
A>>2