INSTITUTE - UIE
DEPARTMENT- ACADEMIC UNIT-2
Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Problem Solving with Programming
Course Code:20CST111
Bitwise Operator and Type Casting DISCOVER . LEARN . EMPOWER
Problem
solving
with
programmi
ng
Course Objectives
The course aims to provide exposure to problem-solving
through programming.
The course aims to raise the programming skills of
students via logic building capability.
With knowledge of C programming language, students
would be able to model real world problems.
2
Problem
solving with
programming
Course Outcome
CO Title Level
Number
CO1 Identifysituations where computational Understand
methods would be useful.
CO2 Approach the programming tasks using Remember
techniques learnt and write pseudo-code.
CO3 Choose the right data representation formats Understand
based on the requirements of the problem.
CO4 Use the comparisons and limitations of the Understand
various programming constructs and choose
the right one for the task.
3
Scheme of Evaluation
Sr. Type of Assessment Weightage of actual Frequency of Task Final Weightage in Internal Remarks
No. Task conduct Assessment (Prorated
Marks)
1. Assignment* 10 marks of One Per Unit 10 marks As applicable to
each assignment course types depicted
above.
2. Time Bound 12 marks for each One per Unit 4 marks As applicable to
Surprise test course types
Test depicted above.
3. Quiz 4 marks of each quiz 2 per Unit 4marks As applicable to
course types
depicted above.
4. Mid-Semester Test** 20 marks for one 2 per semester 20 marks As applicable to
MST. course types
depicted above.
5. Presentation*** Non Graded: Engagement Only for Self Study
Task MNGCourses.
6. Homework NA One per lecture topic Non-Graded: Engagement As applicable to
(of 2 Task course types
questions) depicted above.
7. Discussion Forum NA One per Non Graded: Engagement As applicable to
Chapter Task course types depicted
above.
8. Attendance and NA NA 2 marks
Engagement Score
on BB
4
• Space for visual (size 24)
CONTENTS
• Bitwise operator
• Type Casting/ Type
Conversion
5
Operators
• An operator is a symbol that tells the compiler to perform certain mathematical or
logical manipulations.
• Operators are used in program to manipulate data and variables. The data items
that operators act upon are called operands.
• Some operators require two operands, while others act upon only one operand.
The operators are classified into unary, binary and ternary depending on whether
they operate on one, two or three operands respectively.
11/09/2025 Computer Programming 20CST111 6
Bitwise Operator
• In arithmetic-logic unit (which is within the CPU), mathematical
operations like: addition, subtraction, multiplication and division are
done in bit-level.
• Decimal values are converted into binary values which are the
sequence of bits and bit wise operators work on these bits.
X Y X|Y X&Y X^Y
0 0 0 0 0
0 1 1 0 1
1 0 1 0 1
1 1 1 1 0
Table1: Truth table of Bitwise operations
11/09/2025 Computer Programming 20CST111 7
Bitwise Operator
• Bitwise operators are as follows:
Operator Meaning of operator
& Bitwise And
| Bitwise Or
^ Bitwise XOR
~ Bitwise Compliment
<< Left Shift
>> Right Shift
11/09/2025 Computer Programming 20CST111 8
Bitwise Operator
• Bitwise AND
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bit Operation of 12 and 25
00001100
& 00011001
________
00001000 = 8 (In decimal)
11/09/2025 Computer Programming 20CST111 9
Bitwise Operator
• Bitwise OR
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise OR Operation of 12 and 25
00001100
| 00011001
________
00011101 = 29 (In decimal)
11/09/2025 Computer Programming 20CST111 10
Bitwise Operator
• Bitwise XOR
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise XOR Operation of 12 and 25
00001100
^ 00011001
________
00010101 = 21 (In decimal)
11/09/2025 Computer Programming 20CST111 11
Bitwise Operator
• Bitwise Compliment
35 = 00100011 (In Binary)
Bitwise complement Operation of 35
~ 00100011
________
11011100 = 220 (In decimal)
11/09/2025 Computer Programming 20CST111 12
Bitwise Operator
• Bitwise Right shift
212 = 11010100 (In binary)
212>>2 = 00110101 (In binary) [Right shift by two bits]
212>>7 = 00000001 (In binary)
212>>8 = 00000000
212>>0 = 11010100 (No Shift)
11/09/2025 Computer Programming 20CST111 13
Bitwise Operator
• Bitwise Left shift
212 = 11010100 (In binary)
212<<1 = 110101000 (In binary) [Left shift by one bit]
212<<0 =11010100 (Shift by 0)
212<<4 = 110101000000 (In binary) =3392(In decimal)
11/09/2025 Computer Programming 20CST111 14
Type-Casting/Type-Conversion
• The type conversion process in C is basically converting one type of
data type to other to perform some operation.
• The conversion is done only between those datatypes wherein the
conversion is possible ex – char to int and vice versa.
• It’s of two types
Implicit Type Conversion
Explicit Type Conversion
15
Type-Casting/Type-Conversion
Implicit Type Conversion
• This type of conversion is usually performed by the compiler when necessary
without any commands by the user. Thus it is also called "Automatic Type
Conversion".
• The compiler usually performs this type of conversion when a particular
expression contains more than one data type. In such cases either type promotion
or demotion takes place.
16
Type-Casting/Type-Conversion
Implicit Type Conversion
17
Type-Casting/Type-Conversion
• All the data types of the variables are upgraded to the data type of
the variable with largest data type
• bool -> char -> short int -> int ->
• unsigned int -> long -> unsigned ->
• long long -> float -> double -> long double
18
Type-Casting/Type-Conversion
Implicit Type Conversion
Example 1
int a = 20;
double b = 20.5;
a + b;
Here, first operand is int type and other is of type double. So, as per rule 2, the variable a will be converted to
double. Therefore, the final answer is double a + b = 40.500000.
Example 2
char ch='a';
int a =13;
a + c;
Here, first operand is char type and other is of type int. So, as per rule 1, the char variable will be converted to
int type during the operation and the final answer will be of type int. We know the ASCII value for ch is 97.
Therefore, final answer is a + c = 97 + 13 = 110.
19
Type-Casting/Type-Conversion
Explicit Type Conversion
Explicit type conversion rules out the use of compiler for converting
one data type to another instead the user explicitly defines within the
program the datatype of the operands in the expression.
20
Type-Casting/Type-Conversion
Explicit Type Conversion
21
Type-Casting/Type-Conversion
Explicit Type Conversion Example:
Example:
double da = 4.5;
double db = 4.6;
double dc = 4.9;
//explicitly defined by user
int result = (int)da + (int)db + (int)dc;
printf("result = %d", result);
Output
result = 12
22
Summary
Keep in mind the following rules for programming practice when
dealing with different data type to prevent from data loss :
•Integers types should be converted to float.
•Float types should be converted to double.
•Character types should be converted to integer.
FAQ’s
Q1. Compute the sign of an integer?
Q2. Detect if two integers have opposite signs?
Q3. Write a program to check an integer is a power of 2?
Q4. How to set a particular bit in C?
Q5. When should a type cast not be used?
11/09/2025 Computer Programming 20CST111 24
Assessment
Q1. WAP to clear a bit.
Q2. We define S to be a sequence of distinct sequential integers from 1 to n ; in other words S={1,2,3,
….n}, . We want to know the maximum bitwise AND value of any two integers, a and b (where
a<b ), in sequence S that is also less than a given integer, k . Complete the function in the editor so
that given n and k , it returns the maximum a&b<k .
Note: The &symbol represents the bitwise AND operator.
Sample Input 0
3
52
85
22
Sample Output 0
1
4
0
Write the code of the above scenario that satisfies the above situation.
11/09/2025 Computer Programming 20CST111 25
Assessment
Q 3. What will be the output of the C program?
#include<stdio.h>
int main()
{
int a = 4, b = 2;
printf("a^b = %d", a^b);
return 0;
}
A. 12
B. 10
C. 8
D. 6
11/09/2025 Computer Programming 20CST111 26
Assessment
Q4. What will be the output of the C program?
#include<stdio.h>
int main()
{
int a = 4, b = 2;
printf("a^b = %d", a^b);
return 0;
}
A. 12
B. 10
C. 8
D. 6
Q5. [&] is the symbol of Bitwise And.
Q6. [float to char pointer] type of conversionComputer
11/09/2025 is NOT accepted.
Programming 20CST111 27
References
S.No Title Content link
1 Book Programming in C by Reema Thareja.
2 Book Programming with C (Schaum's Outline Series) by Byron
Gottfried Jitender Chhabra, Tata McGraw Hill.
3 Book The C Programming Language by Brian W. Kernighan, Dennis
Ritchie, Pearson education.
4 Book Programming in ANSI C by E. Balaguruswamy, Tata McGraw
Hill.
5 Weblink https://www.tutorialspoint.com/cprogramming/c_operators.htm
6 Weblink https://www.programiz.com/c-programming
7 Weblink https://fresh2refresh.com/c-programming/
References
S.No Title Content link
8 Weblink https://www.studytonight.com/c/
9 Weblink https://www.javatpoint.com/c-operators
10 Video Link https://www.youtube.com/watch?v=MyxVAq9MifI
11 Video Link https://www.youtube.com/watch?v=xXBitioUzf8
12 Online Course Link https://www.coursera.org/
13 Online Course Link https://www.udemy.com/
14 Online Course Link https://www.niit.com/
THANK YOU