0% found this document useful (0 votes)
11 views

Session 19 Ppt Bitwise Operators2

This session focuses on bitwise operators in programming, including their types and usage. It covers left shift, right shift, and complement operations, demonstrating how they manipulate individual bits of binary numbers. Students will learn to perform bit-level operations and solve related problems through practical examples and exercises.

Uploaded by

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

Session 19 Ppt Bitwise Operators2

This session focuses on bitwise operators in programming, including their types and usage. It covers left shift, right shift, and complement operations, demonstrating how they manipulate individual bits of binary numbers. Students will learn to perform bit-level operations and solve related problems through practical examples and exercises.

Uploaded by

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

Department of BES-1

COMPUTATIONAL THINKING FOR


STRUCTURED DESIGN

Topic: Bit Wise Operators-2

TOPIC
Session - 19

CREATED BY K. VICTOR BABU


AIM OF THE SESSION
To familiarize students with the Bit wise operators and Usage.

INSTRUCTIONAL OBJECTIVES

This Session is designed to:


1 Define types of bitwise operators
2 Understand the different types of bit wise operators.
3 Illustrate the bit wise operators procedure
4 Understand how bit wise procedure will be performed at bit-level.

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

This Session will discuss about …

Bitwise operators are operators that manipulate individual bits of binary


numbers.
These operators perform operations at the bit level, rather than on the whole
numbers. They are used extensively in computer programming for tasks such as
data encryption, compression..

Different type of bit wise operators are


complement (~) operator - flip all the bits of a number.
Left Shift (<<) operator - shifts all bits to the left, add zero to LSB
Right Shift (>>) operator - shifts all bits to the right, add to zero to MSB

CREATED BY K. VICTOR BABU


Session Description
Bit operators works at the bit-level.
<< (left shift) ~bitwise NOT)

>> (right shift)

CREATED BY K. VICTOR BABU


Session Description
Left Shift formula
an number a with an number b denoted as a<<b is equals to multiply a with
2^b =a*2^b.
T Example 5<<2 answer is 5*(2*2)=20 (10100).
Final answer is 10100 (20).
Left shift actual procedure
5<<2
5 denotes 00101 .
00101—5
01010 --- 1st time shift left. (Shift all bits to left side by one position)
10100--- 2nd time shift left
As b says shift 2 times. Final answer is 10100 (20)
CREATED BY K. VICTOR BABU
Session Description
Right Shift using formula
a>>b then will become =a/(2^b).
5>>2
Here a=5 , b=2, according to formula a/(2^b).
5/(2^2)= 5/4 =1. (001)

Right shift actual procedure


5>>2
5-----0101
0010---- 1st time right shift (shift all bits to right side by one position)
0001---- 2nd time right shift
Final answer is 0001 .
CREATED BY K. VICTOR BABU
Session Description

Complement ~ -11011100 (binary


Formal for complement is complement of N =-(N+1). representation of -220
Ex according to formula 2 complement is -(2+1)= -3. here minus (-) Indicates
sign bit MSB).

Complement ~ actual procedure -00100011(flip 0 to 1


35----00100011 and 1 to 0)
1 +( add 1 to
Complement of 35 means above)
11011100(flip bits 0 to 1 and 1 to 0) in decimal answer is 220. -------------------
-00100100
olu ti on =-36 (binary
s
But compiler will give -36 why guess. equivalent).

Because system stores in the 2’s complement of 220. is (-36) . So, complement 35 is -
36.

CREATED BY K. VICTOR BABU


Examples

Example on Bitwise operator

#include <stdio.h>

int main ()

int a = 12, b = 10; tp u t


s Ou
G u es
printf("~a = %d", a = ~a);

printf("b<<1 = %d", b << 1);

printf("b>>1 = %d", b >> 1);

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.

CREATED BY K. VICTOR BABU


SUMMARY

This Session discussed about …

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.

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

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

1 write program to swap a two number using bit wise.

2 write a program to find even or odd using bit wise.

3 write a program to find number of one’s and zero’s in given number


using bit wise.

4 write a program to inverting the bits of given number.

5 write a program to solve the palindrome using bit wise.

6 write a program on flipped bits using bitwise (Ex here a is 8 -- 1000 b is


15 ---1111 to get b .a has to flipped 3 bits)

CREATED BY K. VICTOR BABU


REFERENCES FOR FURTHER LEARNING OF THE SESSION

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.

3. Let Us C : Authentic guide to C programming language (18th Edition)- by Yashavant


Kanetkar BPB publication .

4. C Programming: A Modern Approach by K. N. King 2nd edition - This book is another


excellent
resource for learning C programming, including functions.

Sites and Web links:


1. https://www.codecademy.com/resources/docs/cpp/bitwise-operators
2. https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/
CREATED BY K. VICTOR BABU
THANK YOU

Team – CTSD

CREATED BY K. VICTOR BABU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy