Session 19 Ppt Bitwise Operators2
Session 19 Ppt Bitwise Operators2
TOPIC
Session - 19
INSTRUCTIONAL OBJECTIVES
LEARNING OUTCOMES
At the end of this session, you should be able to:
1 Understand the difference among different bitwise operators.
2 Explains the benefits of Bitwise operators.
3 Demonstrate bit level operations procedure and formula.
4 Solve problems by using Bitwise operators.
CREATED BY K. VICTOR BABU
INTRODUCTION
Because system stores in the 2’s complement of 220. is (-36) . So, complement 35 is -
36.
#include <stdio.h>
int main ()
return 0;
}
CREATED BY K. VICTOR BABU
Examples
Write a program to Find no of 1’s if count even print Ram Wins or Mouni wins
#include<stdio.h>
int main()
int onesCount(int n)
{
{
int num,ans;
int onescount=0;
printf("Enter Any Number=");
while(n>0)
scanf("%d",&num);
{
ans=onesCount(num);
if((n & 1)==1)
if(ans%2==0)
onescount+
printf("Ram wins");
+;
else
printf("Mouni wins");
n=n>>1;
return 0;
} t
} u t pu
return onescount; s s O
Gu e
}
CREATED BY K. VICTOR BABU
ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO THE
SESSION
Group discussion: The session will conclude with a group discussion on the benefits
and types of bitwise operators in C and we ask student to practice on the different
types of bitwise operators.
Oral Quiz- asking Quick MCQ Question on bitwise operators by dividing the whole
class in to 4 groups who get more score that team will win.
One minute Answer: ask the student to write concept within a minute on paper.
Different types of bitwise operators and their usage. By using bitwise we perform
arithmetic operations at bit level. Operators are << left shift , >> right shift ,~ complement .
performs a bitwise NOT (complement) operation on an integer, resulting in a integer with
each bit flipped (1 to 0 and 0 to 1).
shifts the bits of an integer to the left by a specified number of positions, effectively
multiplying the integer by 2 to the power of the shift amount.
shifts the bits of an integer to the right by a specified number of positions, effectively
dividing the integer by 2 to the power of the shift amount.
1. What should be the value of ‘b’ such that the output of the program will be 20?
2. int main()
3. {
4. int a = 5, b = ?;
5. printf("%d\n", a<<b));
6. }
(a) 1
(b) 2
(c) 3
(d) 4
2. Complement of 20
(a) 21
(b) 45
(c) -21
(d) $$ This is comment $$
CREATED BY K. VICTOR BABU
SELF-ASSESSMENT QUESTIONS
1.#include <stdio.h>
2.int main()
3.{
4.if (~0 == 1)
5.printf("yes\n");
6.else
7.printf("no\n");
8.}
(a) yes
(b) no
(c) error
(d) -1
1 int main()
2. {
3. int x = -2;
4. x = x >> 1;
5. printf("%d\n", x);
6. }.
(a) -1
(b) error
(c) 0
CREATED BY K. VICTOR BABU
TERMINAL QUESTIONS
Reference Books:
1. The C Programming Language by Brian Kernighan and Dennis Ritchie 2 nd edition Pearson
publication - This is the classic book on C programming and is a great resource for learning
about functions in C.
2. C: THE COMPLETE REFERENCE McGraw Hill Education; 4th edition by Herb Scheldt.
Team – CTSD