0% found this document useful (0 votes)
21 views86 pages

3.operators - Expressions - TypeConversions V 4.0

The document outlines a mission to bridge the digital skills gap for over 10 million young professionals by training them for careers in technology. It provides a detailed explanation of various operators in the C programming language, including arithmetic, relational, logical, conditional, assignment, and special operators, along with their definitions and examples. The document also discusses the limitations of certain operators, particularly the increment operator.

Uploaded by

2k22it49
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views86 pages

3.operators - Expressions - TypeConversions V 4.0

The document outlines a mission to bridge the digital skills gap for over 10 million young professionals by training them for careers in technology. It provides a detailed explanation of various operators in the C programming language, including arithmetic, relational, logical, conditional, assignment, and special operators, along with their definitions and examples. The document also discusses the limitations of certain operators, particularly the increment operator.

Uploaded by

2k22it49
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 86

We are on a mission to address the digital

skills gap for 10 Million+ young professionals,


train and empower them to forge a career
path into future tech
Operators, Expressions and
TypeConversions
Operators
Please download pictures in
suitable size here and insert them
by clicking the symbol above.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Operators

Operators in C is a special symbol used to perform the mathematical and logical computations.

– Operands: The data items on which the operators are applied.

– Operators: Applied between the operands

Depending on the number of operands, operators are classified as follows:

• Unary Operator : It is applied to the single operand.

• Binary Operator: It is applied between two operands.

• Ternary Operator: Conditional operator in C having 3 operands.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Operators

Binary operators are classified as shown below

Types of Operators

Arithmetic Relational Logical Bitwise Conditional Assignment Special


Operators Operators Operators Operators Operators Operators Operators

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Arithmetic Operators

✓ The arithmetic operators are used to perform arithmetic/mathematical operations on operands.

Operator Operation Description Example


+ Addition​ Adds the two numeric values on either num1 = 5​;
side of the operator​ num2 = 6​;
printf(“%d”,(num1 + num2)​);
11​
- Subtraction Subtracts the two numeric values on either num1 = 5​;
side of the operator​ num2 = 6​;
printf(“%d”,(num1 - num2)​);
-1
* Multiplication Multiples the two numeric values on either num1 = 5​;
side of the operator​ num2 = 6​;
printf(“%d”,(num1 * num2)​);
30

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Arithmetic Operators

✓ The arithmetic operators are used to perform arithmetic/mathematical operations on operands.

Operator Operation Description Example


/ Division​ Divides the operand on the left by the num1 = 8​;
operand on the right and returns the num2 = 4​;
quotient​ printf(“%d”,(num1 / num2)​);
2
% Modulus​ Divides the operand on the left by the num1 = 13​;
operand on the right and returns the num2 = 5​;
remainder.​ printf(“%d”,(num1 % num2)​);
3​
num1 = 13;
+ Unary Plus Used to specify the positive values. printf(“%d”,num1);
​13

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Arithmetic Operators

✓ The arithmetic operators are used to perform arithmetic/mathematical operations on operands.

Operator Operation Description Example


num1 = -13;
- Unary Minus Flips the sign of the value. printf(“%d”,num1);
-13
PostIncrement
num1 = 13;
num2 = num1++;
printf(“%d”,num2,num1)
​13 14
++ Increment Increases the value of the operand by 1.
PreIncrement
num1 = 13;
num2 = ++num1;
printf(“%d”,num2,num1)
​14 14

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Arithmetic Operators

✓ The arithmetic operators are used to perform arithmetic/mathematical operations on operands.

Operator Operation Description Example


PostDecrement
num1 = 13;
num2 = num1--;
printf(“%d”,num2,num1)
​13 12
-- Decrement Decreases the value of the operand by 1.
PreDecrement
num1 = 13;
num2 = --num1;
printf(“%d”,num2,num1)
​12 12

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Limitations of Increment Operator

1. It cannot be applied to constants, expressions, or non-modifiable values.

Int a = 5;
++a ; // Valid : Increments the variable 'a'
7++; // Invalid : Cannot increment a constant
(a+1)++; // Invalid : Cannot increment an expression

2. Undefined Behavior in Certain Situations (Multiple Increments in a Single Statement)

Limitation 2:
Int x = 5;
X=x++ + ++x; // Undefined behaviour : The results may vary depends upon the compiler

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Limitations of Increment Operator

3. Cannot Increment Read-Only Variables (e.g., const):

Const Int a = 10;


a++; // Invalid: Cannot increment a constant variable

4. Cannot be Used with Non-Scalar Data Types: Increment operators can only be applied to scalar data
types (i.e., integers, floating-point numbers, pointers). You cannot use increment operators on arrays,
structures, or functions.
Struct myStruct {
Int a;
Int arr[5] = {1,2,3,4,5};
};
++arr; // Invalid: Cannot increment array
Struct myStruct s;
s++; // Invalid: Cannot increment a
Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0
Structure
Operators

Types of Operators : Relational Operators

• The relational operators in C are used for the comparison of the two operands.

• All these operators are binary operators that return true or false values as the result of comparison.

Operator Operation Description Example

num1 = 8​;num2 = 4​;


printf(“%d”,(num2 < num1)​);
If the value of the left-side operand is greater than
1
< Less than the value of the right-side operand, then condition
num1 = 8​;num2 = 4​;
is True, otherwise it is False
printf(“%d”,(num1 < num2)​);
0
num1 = 8​;num2 = 4​;
printf(“%d”,num2 > num1)​);
If the value of the left-side operand is greater than
0
> the value of the right-side operand, then condition
Greater than num1 = 8​;num2 = 4​;
is True, otherwise it is False
printf(“%d”,(num1 > num2)​);
1

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Relational Operators

Operator Operation Description Example

num1 = 8​;num2 = 8;
printf(“%d”,(num2 <= num1)​);
If the value of the left operand is less than or
Less than or 1
<= equal to the value of the right operand, then is
equal to num1 = 8​;num2 = 4​;
True otherwise it is False
printf(“%d”,(num1 <= num2)​);
0
num1 = 4​;num2 = 4​;
If the value of the left-side operand is greater printf(“%d”,(num2 >= num1)​);
than or equal to the value of the right-side 1
>= Greater than or
operand, then condition is True, otherwise it is num1 = 3​;num2 = 4​;
equal to
False printf(“%d”,(num1 >= num2)​);
0

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Relational Operators

Operator Operation Description Example

num1 = 8​;num2 = 4​;


If the values of two operands are equal, then
== Equal To printf(“%d”,(num2 == num1)​);
the condition is True, otherwise it is False
0
num1 = 8​;num2 = 4​;
If values of two operands are not equal, then
!= Not equal to​ printf(“%d”,(num2 != num1)​);
condition is True, otherwise it is False​
1

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Logical operators


• Logical Operators are used to combine two or more conditions/constraints or to complement the
evaluation of the original condition in consideration.

• The result of the operation of a logical operator is a Boolean value either true or false.

Operator Operation Description Example

num1 = 8​;num2 = 4;num3 = 5;


Returns true if both the num4 =( num1 > num2) &&( num1>num3);
&& Logical AND
operands are true. printf(“%d”,num4​);
1
num1 = 8;num2 = 4;num3 = 5;
Returns true if both or any of the num4 =( num1 > num2) || ( num2 >num3);
|| Logical OR
operand is true. printf(“%d”,num4​);
1

num1 = 8; num1 = 0
Returns true if the operand is
! Logical NOT printf(“%d”,!num1​); printf(“%d”,!num​1);
false.
0 1
Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0
Operators

Types of Operators : Conditional operators

• The conditional operator, often referred to as the "ternary operator“, takes three operands: a condition,
a value to return if the condition is true, and a value to return if the condition is false.

• Syntax:

expr1 ? expr2 : expr3


• The expression expr1 is evaluated first.

• If it is non-zero (true), then the expression expr2 is evaluated, and that is the value of the conditional
expression. Otherwise, expr3 is evaluated, and that is the value. Only one of expr2 and expr3 is
evaluated.

Example:

z = (a > b) ? a : b; /* z = max(a, b) */ is equal to if (a>b)

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Assignment operators

• Assignment operators are used to assign value to a variable.

• The left side operand of the assignment operator is a variable and the right side operand of the

assignment operator is a value.

• The value on the right side must be of the same data type as the variable on the left side otherwise the

compiler will raise an error.

• The assignment operators can be combined with some other operators in C to provide multiple

operations using single operator. These operators are called compound operators or short-hand

operators

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Assignment operators

Operator Description Example

num1 = 8​;num2 = num1;


= Assigns value from right-side operand to left-side operand printf(“%d”,num2);
8

num1 = 8​;num1 += 2;
It adds the value of right-side operand to the left-side
printf(“%d”,num1);
+= operand and assigns the result to the left-side operand​
10
Note: x += y is same as x = x + y

It subtracts the value of right-side operand from the left- num1 = 8​;num1 -= 2;
-= side operand and assigns the result to left-side operand​ printf(“%d”,num1);
Note: x -= y is same as x = x - y 6

It multiplies the value of right-side operand with the value


num1 = 8​;num1 *= 2;
of left-side operand and assigns the result to left-side
*= printf(“%d”,num1);
operand​
16
Note: x *= y is same as x = x * y
Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0
Operators

Types of Operators : Assignment operators

Operator Description Example

It divides the value of right-side operand with the value of left- num1 = 8​;num1 /= 2;
/= side operand and assigns the result to left-side operand​ printf(“%d”,num1);
Note: x /= y is same as x = x / y 4

num1 = 8​;num1 %= 2;
It performs modulus operation using two operands and
printf(“%d”,num1);
%= assigns the result to left-side operand​
0
Note: x %= y is same as x = x % y

It performs bitwise AND operation using two operands and num1 = 8​;num1 &= 2;
&= assigns the result to left-side operand​ printf(“%d”,num1);
Note: x &= y is same as x = x & y 0

It performs bitwise OR operation using two operands and num1 = 8​;num1 |= 2;


|= assigns the result to left-side operand​ printf(“%d”,num1);
Note: x |= y is same as x = x | y 10

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Assignment operators

Operator Description Example

num1 = 8​;num1 ^= 2;
It performs bitwise OR operation using two operands
printf(“%d”,num1);
^= and assigns the result to left-side operand​
10
Note: x ^= y is same as x = x | y

num1 = 8​;num1 >>= 2;


It Performs bitwise Rightshift and assign this value to the
>>= printf(“%d”,num1);
left operand.
2

num1 = 8​;num1 <<= 2;


It Performs bitwise Leftshift and assign this value to the
<<= printf(“%d”,num1);
left operand.
32

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Special operators

sizeof Operator

• The sizeof operator in C is a compile-time operator that is used to determine the size (in bytes) of a

data type or a variable.

• It evaluates to the size, in bytes, of the operand. The operand can be a variable, a data type, or an

expression.

• Syntax

sizeof(variable/datatype/expression)

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Special operators

The Comma Operator

• The comma operator can be used to link the related expressions together.

• A comma-linked list of expressions is evaluated left to right and the value of right-most expression is the

value of the combined expression.

Example: value = (x=10, y=5, x+y);

✓ First assigns the value 10 to x, then assigns 5 to y, and finally assigns 15 to value.

✓ Since comma operator has the lowest precedence of all operators, the parentheses are necessary.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Special operators

Dot (.) and Arrow (->) Operators

• Member operators are used to reference individual member of structures and unions.
• The dot operator is used to access members of a structure or union.

• The arrow operator is used to access members of a structure or union through pointer. an
object.
Example: Example:
struct Point { struct Point {
int x; int x;
int y; int y;
}; };
struct Point p1; struct Point *p2 = & p1;
p1.x = 10; // Using dot operator to access and set P1->x = 10; // Using dot operator to access and set
value value
p1.y = 20; P1->y = 20;

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Special operators

Cast Operator

• The cast operator is used to convert a variable from one data type to another explicitly.

• It is typically used when you need to perform type conversions, especially between incompatible

data types, or when you want to control how a value is treated during operations.

• Syntax

(type)expression
• ct.
Example: float num = 3.14;

int x = (int) num //Casts float to int, truncating the decimal part (result: 3)
Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0
Operators

Types of Operators : Special operators

Address-Of (&)

• The Address-Of Operator (&) is used to obtain the memory address of a variable. When applied to

a variable, it returns the address where the variable is stored in memory.

• Syntax

&variable

Example:int num = 10;

int *ptr = &num; // 'ptr' stores the address of 'num'

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Special operators

Dereference (*) Operators

• Used to access the value stored at the memory address pointed to by a pointer.

• In other words, it "dereferences" the pointer to retrieve or modify the actual data at that memory

address.

• Syntax

*variable

Example: int num = 10;


int *ptr = &num; // 'ptr' stores the address of 'num’
Int value = *ptr // Dereferences 'ptr' to get the value of 'num'
Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0
Operators

Types of Operators : Bitwise operators

• Bitwise operators are used to perform operations at the bit level and help to manipulate data at the

bit level which we can call bit-level programming.

• Bit-level programming contains 0 and 1.

• These can be done by first converting a decimal value to its binary form. This binary form is nothing

but a sequence of bits.

• Bitwise operators perform operations on these bits that has constant time complexity.

• Bitwise operators operate on integers and characters but not on data types float or double.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

Computer programming tasks that require bit manipulation include:

• Low-level device control

• Error detection and correction algorithms

• Data compression

• Encryption algorithms

• Optimization

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

BITWISE operator Vs Logical Operator:

• The logical operators work with Boolean data and return a Boolean value, i.e. True or False.

• The bitwise operators in C work with integers, i.e. they take integer inputs, manipulate with their bit

and return an integer value.

• The bitwise AND, and OR use ‘&’ and ‘|’ as their operators, while the logical AND, and OR use ‘&&’

and ‘||’ as their operators.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

Example
Operator Operation Description
(a=5, b=6)
Performs bit-by-bit AND operation and
& Bitwise AND a&b:4
returns the result.

Performs bit-by-bit OR operation and


| Bitwise OR a|b:7
returns the result.

Performs bit-by-bit XOR operation and


^ Bitwise XOR a^b:3
returns the result.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

Example
Operator Operation Description
(a=5, b=6)
Bitwise First Flips all the set and unset bits on the
~ ~a : -6
Complement number.

Shifts the number in binary form by one


<< Bitwise Leftshift place in the operation and returns the a << 2 : 20
result.

Shifts the number in binary form by one


>> Bitwise Rightshilft place in the operation and returns the a >> 2 : 1
result.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

Truth table for Bitwise AND operator


• Bitwise AND(&) is a binary operator that operates on two
X Y X&Y
equal-length bit patterns.
0 0 0
1 0 0
• If both bits in the compared position of the bit patterns
0 1 0
are 1, the bit in the resulting bit pattern is 1, otherwise 0. 1 1 1

Example
int result, X = 5, Y = 6; Output:
X :510 => (101)2 Bitwise AND:(X & Y): 4
result = X & Y;
Y :610 => (110)2
printf(“Bitwise AND:(X & Y): %d", result);
X&Y : (101)2 & (110)2 => (100)2 (which is 410 )

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

Truth table for Bitwise OR operator


• Bitwise OR is also a binary operator that operates on two
X Y X|Y
equal-length bit patterns, similar to bitwise AND.
0 0 0
1 0 1
• If both bits in the compared position of the bit patterns are
0 1 1
0, the bit in the resulting bit pattern is 0, otherwise 1. 1 1 1

Example
int result, X = 5, Y = 6; Output:
X :510 => (101)2 Bitwise OR:(X | Y): 4
result = X | Y;
Y :610 => (110)2
printf(“Bitwise OR:(X | Y): %d", result);
X | Y :(101)2 | (110)2 => (111)2 (which is 710 )

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

Truth table for Bitwise XOR operator


• Bitwise XOR also takes two equal-length bit patterns.
X Y X^Y
• If both bits in the compared position of the bit patterns are 0 0 0 0
1 0 1
or 1, the bit in the resulting bit pattern is 0, otherwise 1.
0 1 1
1 1 0

Example
int result, X = 5, Y = 6; Output:
X :510 => (101)2 Bitwise XOR:(X ^ Y): 3
result = X ^ Y;
Y :610 => (110)2
printf(“Bitwise XOR:(X ^ Y): %d", result);
X ^ Y :(101)2 ^ (110)2 => (011)2 (which is 310 )

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

• Bitwise NOT is an unary operator that flips the bits of the Truth table for Bitwise NOT operator
X ~X
number i.e., if the ith bit is 0, it will change it to 1 and vice
0 1
versa. 1 0

• Bitwise NOT is nothing but simply the one’s complement of a

number.

int result, X = 5 ; Output:


Example Bitwise NOT:~(X): -6
result = ~ X;
X :510 => (101)2
printf(“Bitwise NOT :~(X): %d", result);
~X : ~(101)2 => (010)2 (which is 210 )

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

• Here, logically ~5 = 2 but, the compiler returns the 2’s complement of the input value.

• In C, integers are typically stored in two's complement form, which means that negative numbers

are represented in a binary form where the most significant bit (MSB) is the sign bit (1 for

negative, 0 for non-negative).

• When you perform bitwise NOT (~) on a positive integer, you get a result in two's complement

form.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

• The value of X is 5, which is represented as 0000 0101 in binary (assuming 8-bit integers for

simplicity).

• When you apply bitwise NOT, you flip each bit: Bitwise NOT of 0000 0101 is 1111 1010.

• Now, if you interpret 1111 1010 as a two's complement signed integer, it represents -6. That's why

you see the result as -6, not 6.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

• If you were expecting a positive result, you may want to use an unsigned integer type.

#include <stdio.h>
int main() {
unsigned int X = 5;
unsigned int result = ~X;
printf("Bitwise NOT of %u is %u\n", X, result);
return 0; Output:
Bitwise NOT of 5 is 4294967290
}

• The result you're seeing, "4294967290," is the decimal representation of the bitwise NOT operation on

the unsigned integer 5 when using 32 bits.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

• The left shift operator (<<) is a bitwise operator that shifts the bits of a binary number to the left by

a specified number of positions.

• In other words, it multiplies the number by 2 raised to the power of the shift count.

• Here's how the left shift operation works:

– Each bit in the binary representation of the number is shifted to the left by the specified number of

positions.

– The vacant positions on the right are filled with zeros.

– The leftmost bits that are shifted out (if any) are discarded.
Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0
Operators

Types of Operators : Bitwise operators

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

#include <stdio.h> Example:


int main() { Original number : 0000 0101 (5 in decimal)
int X = 5; Left shift by 2 (5<<2) : 0001 0100 (20 in decimal)
int result = X << 2;
printf("Left shift of %d by 2 is %d\n", X, result);
return 0;
} Output:
Left shift of 5 by 2 is 20

• In the example, the value of X is 2, and the result of X << 2 is 20.

• The binary representation of 5 is 0000 0101, and after left-shifting by 2 positions, it becomes 0001

0100, which is 20 in decimal.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

• The right shift operator (>>) is a bitwise operator that shifts the bits of a binary number to the right
by a specified number of positions.

• In other words, it divides the number by 2 raised to the power of the shift count, discarding the
remainder.

• Here's how the right shift operation works:

– Each bit in the binary representation of the number is shifted to the right by the specified number
of positions.

– The vacant positions on the left are filled with the sign bit (for signed integers) or with zeros (for
unsigned integers).

– The rightmost bits that are shifted out (if any) are discarded.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Types of Operators : Bitwise operators

Example:
#include <stdio.h>
Original number : 0000 0101 (5 in decimal)
int main() { Right shift by 2 (5 >>2) : 0000 0001 (1 in decimal)
int X = 5;
int result = X >> 2;
printf("Right shift of %d by 2 is %d\n", X, result);
return 0; Output:
} Right shift of 5 by 2 is 1

• In the example, the value of X is 5, and the result of X>> 2 is 1.

• The binary representation of 5 is 0000 0101, and after right-shifting by 2 positions, it becomes

0000 0001, which is 1 in decimal.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Applications of Bitwise operators

• Bitwise operators are commonly employed in low-level programming for direct manipulation of

bits.

• They provide highly efficient operations, making them especially valuable in performance-critical

applications such as embedded systems, networking, and graphics.

• Some of the key use cases are:

• Testing of bits (&)

• Setting of bits (|)

• Clearing of bits(~ and &)

• Toggling of bits (^)


Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0
Operators

Applications of Bitwise operators

• Bit Masking is a technique used in programming to manipulate specific bits in a number using

bitwise operations.

• It involves applying a bitwise mask (a binary number composed of 0s and 1s) to select, modify,

or clear certain bits in another binary number.

• The mask allows you to isolate, set, or clear specific bits without altering the other bits in the

number.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Applications of Bitwise operators

• Here we have to decide what should be the Mask_value, and then take that Mask_value
and do a bitwisewise AND(&) operation with a given data to get the output.

Example: Check whether the required bit position of data is 0 or 1


Data is masked with zeros to make all
bits of the data in the area zero at the
[Input] number (46) 001011102 output
& &
[Mask] Mask_value (01) 000000012

[Output] 000000002
Data is masked with 1 to test LSB of
data

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Applications of Bitwise operators

Testing of bit

• Testing a bit refers to checking the value of a specific bit within a binary representation of a number

or data.

• In the context of programming and bitwise operations, testing a bit typically involves checking whether a

particular bit is set (equal to 1) or clear (equal to 0) within a binary value.

• Lets us understand with the below example:

Write a program to find out whether a user entered a number is even or odd. Print an appropriate message on
the console. (Use testing of bits logic)

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Applications of Bitwise operators

Logic & Code:


• The least significant bit (LSB) of a binary number determines if it is odd or even. Bitwise AND (&) with 1
can quickly determine this.
• If (n & 1) == 0, the number is even.
• If (n & 1) == 1, the number is odd.
#include <stdio.h>
int main() {
int n = 5;
(n & 1 == 1) ? printf(“Odd number”) : printf(“Even Number”);
return 0; Output:
} Odd Number

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Applications of Bitwise operators

Setting of bit
• To set a particular bit to 1, you can use the OR (|) operator with a mask that has 1 in the position of
the bit you want to set.

• Lets us understand with the below example:

#include <stdio.h>
int main() {
int n = 8; // Binary: 00001000
int mask = 1 << 2; // Binary: 00000100 (mask to set the 3rd bit)
n = n | mask; //Binary : 00001100 ( 3rd bit is set)
prinftf(“%d”,n);
Output:
return 0;
} 12
Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0
Operators

Applications of Bitwise operators

Clearing of bit
• To clear a specific bit (set it to 0), you can use the AND (&) operator with the NOT (~) of a mask that has 1 at the
bit position you want to clear.

• Lets us understand with the below example:

#include <stdio.h>
int main() {
int n = 12; // Binary: 00001100
int mask = ~(1 << 2); // Binary: ~(0000100) => 1111011(mask to clear the 3rd bit)
n = n & mask; //Binary : 00001000 ( 3rd bit is cleared)
prinftf(“%d”,n);
Output:
return 0;
}
8

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators

Applications of Bitwise operators

Toggling of bit
• To toggle a specific bit (change its value from 0 to 1 or 1 to 0), use the XOR (^) operator with a mask that has 1
at the bit you want to toggle

• Lets us understand with the below example:

#include <stdio.h>
int main() {
int n = 5; // Binary: 00000101
int mask = 1 << 1; // Binary: 00000010 (mask to toggle the 2nd bit)
n = n ^ mask; //Binary : 00000111 ( 2nd bit is toggled)
prinftf(“%d”,n);
return 0; Output:
} 7

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Expressions
Please download pictures in
suitable size here and insert them
by clicking the symbol above.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Expressions

Expressions

• An expression in C is any valid combination of tokens like variables, constants and operators.

• An expression may consist of one or more operands, and zero or more operators to produce a

value.

Examples:

a+b*c

(a*b)/(c+d)

++a&&b++||c

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Expressions

Expressions

Expression Evaluation:

• The process of simplifying or computing the value of an expression is determined by the

specified operations, following rules for operator precedence, associativity, and data types.

• A value or a standalone variable is also considered as an expression but a standalone operator is

not an expression.

# Valid Expressions
(i) 100 (iv) 3.0 + 3.14
(ii) num (v) 23/3 -5 * 7(14 -2)
(iii) num – 20.4 (vi) "Global" + "Citizen"

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Expressions

Expressions

Key Concepts in Expression Evaluation:

1. Operator Precedence

• It determines which operator is performed first in an expression with more than one operators

with different precedence.

2. Operators Associativity

• When two operators of the same precedence appear in an expression, associativity determines

the order of evaluation. Associativity can be either Left to Right or Right to Left.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Expressions

Operators Precedence & Associativity

Precedence Operator Description Associativity

() Parentheses (function call) left-to-right

[] Brackets (array subscript) left-to-right

1. . Member selection via object name left-to-right

-> Member selection via a pointer left-to-right

Postfix increment/decrement (a is a
a++ , a– left-to-right
variable)

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Expressions

Operators Precedence & Associativity

Precedence Operator Description Associativity

Prefix increment/decrement (a is a
++a , –a right-to-left
variable)

+,– Unary plus/minus right-to-left

!,~ Logical negation/bitwise complement right-to-left

Cast (convert value to temporary value of


2. (type) right-to-left
type)

* Dereference right-to-left

& Address (of operand) right-to-left

Determine size in bytes on this


sizeof right-to-left
implementation

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Expressions

Operators Precedence & Associativity

Precedence Operator Description Associativity

3. *,/,% Multiplication/division/modulus left-to-right

4. +,– Addition/subtraction left-to-right

5. << , >> Bitwise shift left, Bitwise shift right left-to-right

Relational less than/less than or equal


< , <= left-to-right
to
6.
Relational greater than/greater than or
> , >= left-to-right
equal to

7. == , != Relational is equal to/is not equal to left-to-right

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Expressions

Operators Precedence & Associativity

Precedence Operator Description Associativity

8 & Bitwise AND left-to-right

9 ^ Bitwise exclusive OR left-to-right

10 | Bitwise inclusive OR left-to-right

11 && Logical AND left-to-right

12 || Logical OR left-to-right

13 ?: Ternary conditional right-to-left

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Expressions

Operators Precedence & Associativity

Precedence Operator Description Associativity

= Assignment right-to-left

+= , -= Addition/subtraction assignment right-to-left

*= , /= Multiplication/division assignment right-to-left


14. %= , &= Modulus/bitwise AND assignment right-to-left

Bitwise exclusive/inclusive OR
^= , |= right-to-left
assignment

<<=, >>= Bitwise shift left/right assignment right-to-left

15. , expression separator left-to-right

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Expressions

Expressions – Example 1

How will C evaluate the following expression ?

Expression:15.0 / 4 + (8 + 3.0)

Solution:
= 15.0 / 4 + (8.0 + 3.0) (Expression within parenthesis is evaluated first)
=15.0 / 4.0 + 11.0 (Performs division operation)
=3.75 + 11.0
=14.75

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Expressions

Expressions – Example 2

How will C evaluate the following expression ?

Expression: 17 – 8 /4 * 2 + 3 - ++5

Solution:
= 17- 8 / 4 * 2 + 3 - ++5 (Performs Pre increment operation )
= 17- 8 / 4 * 2 + 3 - 6 (Performs division operation – left to right Associativity)
= 17- 2 * 2 + 3 – 6 (Performs multiplication operation – left to right Associativity)
= 17- 4 + 3 – 6 (Performs subtraction operation – left to right Associativity)
= 13 + 3 – 6 (Performs addition operation – left to right Associativity)
= 16 - 6
= 10

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Expressions

Expressions – Example 3

How will C evaluate the following expression ?

Expression: m = i + ( j = 2 + K) where i = 4, j = 2, k = 6, a = 2

Solution:
m = 4 + ( j = 2 + 6);
m = 4 + ( j = 8);
m=4+8;
m = 12

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Type Conversions
Please download pictures in
suitable size here and insert them
by clicking the symbol above.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Type Conversion

Type Conversion

• Type conversion in C is the process of converting one data type to another.

• The type conversion is only performed to those data types where conversion is possible, by a

compiler.

• Type conversion is done at compile time and it is also called widening conversion because the

destination data type can’t be smaller than the source data type.

• There are two types of Conversion:

• Implicit Conversion

• Explicit Conversion

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Type Conversion

Implicit Type Conversion

• Known as ‘automatic type conversion’.

• Done by the compiler on its own, without any external trigger

from the user.

• Generally takes place when in an expression more than one

data type is present. In such conditions type conversion (type

promotion) takes place to avoid loss of data.

• All the data types of the variables are upgraded to the data type

of the variable with the largest data type.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Type Conversion

Implicit Type Conversion

bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long

double

• It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly

converted to unsigned), and overflow can occur (when long is implicitly converted to float).

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Type Conversion

Implicit Type Conversion - Example

#include <stdio.h>
int main() {
int x = 10; // integer x
char y = 'a'; // character c
// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x + y;
// x is implicitly converted to float
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
return 0;
Output:
}
x = 107, z = 108.000000

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Type Conversion

Explicit Type Conversion

• This process is also called type casting and it is user-defined.

• Here the user can typecast the result to make it of a particular

data type.

Syntax:

(type) expression

• Type indicated the data type to which the final result is

converted.

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Type Conversion

Explicit Type Conversion - Example

// C program to demonstrate explicit type Conversion


#include<stdio.h>
int main() {
double x = 1.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
printf("sum = %d", sum);

return 0;
} Output:
sum = 2

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators,Expressions and TypeConversions

Quiz
1. What will be output if you execute following c code?
#include<stdio.h>
int main()
{
printf("%d\t",sizeof(2.5));
printf("%d\t",sizeof(2));
printf("%d",sizeof('A'));
return 0;
}

a) 8 4 4 b) 8 4 2

c) 4 4 1 d) 8 4 1

a) 8 4 4

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators,Expressions and TypeConversions

Quiz
2. What will be output if you execute following c code?

#include<stdio.h>
int main(){
int num = - -2;
printf("num = %d", num);
return 0;
}

a) -2 b) Compilation Error

c) Runtime error d) 2

d) 2

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators,Expressions and TypeConversions

Quiz
3. What will be output if you execute following c code?

#include<stdio.h>
int main()
{
int x=3, y=4, z=4;
printf("%d", (z>=y>=x?100:200));
return 0;
}

a) 100 b) 200

c) 0 d) 1

b) 200

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators,Expressions and TypeConversions

Quiz
4. What will be output if you execute following c code?
#include<stdio.h>
int main()
{
int a=30, b=40, x;
x=(a!=10) && (b=50);
printf("%d",x);
return 0;
}

a) 1 b) 2

c) 3 d) 4

a) 1

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators,Expressions and TypeConversions

Quiz
5. What will be output if you execute following c code?
#include<stdio.h>
int main()
{
int i;
(i=8)+=1;
printf("%d",i);
return 0;
}

a) 9 b) 10

c) 32 d) Compilation Error

d) Compilation Error

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators,Expressions and TypeConversions

Quiz
6. What will be output if you execute following c code?
#include<stdio.h>
int main()
{
int a = 2,b = 5;
a = a^b;
b = b^a;
printf("%d %d",a,b);
return 0;
}

a) 5 2 b) 2 5

c) 7 7 d) 7 2

d) 7 2

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators,Expressions and TypeConversions

Quiz
7. What will be output if you execute following code?
#include <stdio.h>

int main() {
int x = 10;
int y = (x++, x++, x++);
printf("%d %d\n", x, y);
return 0;
}

a) 13 12 b) 13 13

c)10 10 d) Compiler Dependent

d) 13 12

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators,Expressions and TypeConversions

Quiz
8. For a given integer, which of the following operators can be
used to “set” and “reset” a particular bit respectively?

a) | and & b) && and ||

c)& and | d) || and &&

a) | and &

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators,Expressions and TypeConversions

Quiz
9. What will be output if you execute following code?

#include <stdio.h>
int main()
{
printf("%d", 1 << 2 + 3 << 4);
return 0;
}

a) 112 b) 52

c) 512 d) 0

c) 512

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators,Expressions and TypeConversions

Quiz
10. What will be output if you execute following code?
#include <stdio.h>
int main(void)
{
int a = 9;
float x;
x = a / 2;
printf("%f", x);
return 0;
}

a) 4.000000 b) 0

c) 4 d) 4.500000

a) 4.000000

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators,Expressions and TypeConversions

Quiz
11. What will be output if you execute following code?
#include <stdio.h>
int main()
{
int i = -3,j = 2,k= 0 ,m;
m = ++i && ++j || ++k ;
printf("%d %d %d %d",i,j,k,m);
return 0;
}

-2 3 0 1

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators,Expressions and TypeConversions

Quiz
12. What will be output if you execute following code?
#include <stdio.h>
int main()
{
int i = -3,j = 2,k= 0 ,m;
m = ++i || ++j || ++k ;
printf("%d %d %d %d",i,j,k,m);
return 0;
}

-2 2 0 1

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators,Expressions and TypeConversions

Quiz
13. What will be output if you execute following code?
#include <stdio.h>
int main()
{
int i = -3,j = 2,k= 0 ,m;
m = ++i && ++j && ++k ;
printf("%d %d %d %d",i,j,k,m);
return 0;
}

-2 3 1 1

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


Operators,Expressions and TypeConversions

Quiz
14. What will be output if you execute following code?

#include<stdio.h>
void main(){
int i = 2;
int j = i + (1,2,3,4,5);
printf("%d",j);
}

Operators,Expressions and TypeConversions| ©SmartCliff | Version 3.0


THANK YOU

You might also like