0% found this document useful (0 votes)
46 views8 pages

Session 19 Bitwise Operators2

This session focuses on bitwise operators in C, including left shift, right shift, and complement operations. Students will learn the definitions, procedures, and applications of these operators, as well as how to solve problems using them. The session also includes practical programming examples and discussions on the significance of bitwise operations in computing.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views8 pages

Session 19 Bitwise Operators2

This session focuses on bitwise operators in C, including left shift, right shift, and complement operations. Students will learn the definitions, procedures, and applications of these operators, as well as how to solve problems using them. The session also includes practical programming examples and discussions on the significance of bitwise operations in computing.

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 PDF, TXT or read online on Scribd
You are on page 1/ 8

Session 19 -Bit Wise Operators-2

1. Aim of the Session


To familiarize students with the Bit wise operators and Usage.

2. 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. Demonstrate Programs with Bitwise operators.

3. 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. Understand the requirement of bitwise operators in problem solving.
4. Demonstrate bit level operations procedure and formula.
5. Solve problems by using Bitwise operators.
6. Use bit wise operators which solves some Programs Fast (like EVEN or ODD, swap of two
numbers).

4. Session Introduction

In this session we will discuss about

Concept of Bitwise operators like Left shift <<, Right shift >> ,bitwise complement ~
Procedure to solve the left, right shift operators and complement.
Formula to solve left, right shift operators and complement.
Some programs related to Bit wise operator.
How Negative numbers are represented 2’s complement.

5. Session Description
Bit operators works at the bit-level. we have some bit wise operators like below

(left shift) << in C requires two arguments; the first argument determines how many times
to left shift, and the second argument determines how many bits to left shift.
(right shift)>> in the C language that accepts two arguments, right shifts the first argument's
bits, and determines how many times to right shift using the second operand.

~ complement NOT in C takes one argument and flips all bits of it.

Left Shift <<

Left shift formula. And Actual Procedure

an number a with an number b denoted as a<<b is equals to multiply a with 2^b


=a*2^b.

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

Right Shift >>

Right shift formula. And Actual Procedure

a=5; which is 101 Now, if “a is right-shifted by 2”

a=a>>2 then a will become a=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 .

Complement ~ -- formula and actual procedure

We store the negative number is in two’s complement.

2’s Complement:
Two’s complement is operation on bits of specific number. The 2’s complement of a number
is equal to the complement of that number plus 1.

5---0101
1’s complement for 5 is 1010( flip bits 1 to 0 and 0 to 1)’
1s complement of 5 is 1010 plus 1
1010
1+
------------
1011----- decimal 11.
So 2’s complement 5 is 11.

the bitwise complement of X is -(X+1) here X is number.


complement of X = ~X(represented in 2’s complement form).
2’s complement of ~X= -(~(~X)+1) = -(X+1).

Formal for complement is complement of N =-(N+1).

Ex 2 complement is -(2+1)= -3.

Actual procedure

35----00100011

Complement of 35 means

11011100(flip bits 0 to 1 and 1 to 0) in decimal answer is 220.

But when you execute in compiler, we will get answer as -36. How???

Because compiler is returning the 2’s complement of 220. (-36) .

-11011100 (binary representation of -220 here minus (-) Indicates sign bit MSB).
-00100011(flip 0 to 1 and 1 to 0)
1 +( add 1 to above)
-------------------
-00100100
=-36 (binary equivalent).

So, complement 35 is -36.

Additional point about bit wise operator.

1. The left-shift and right-shift operators will not work with negative numbers. If you use
which gives wrong result.

2. If the number is shifted more than the size of the integer, the result is wrong For

Ex 1<<35 is wrong if integer takes 32 bits.

3. Formula for left shift is 1<<7 = 1*pow (2,7)). formula for right shift is division of the first
argument and second argument raise to power of 2 (1>>8 = 1/pow(2,8))

Programs on bit wise operator.

#include <stdio.h>

int main ()

int a = 12, b = 10;

printf("~a = %d", a = ~a);

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

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

return 0;

}
Program to count the number of 1’s in given number if count is even the print ram wins or
ravi wins. ( Right shift example)

#include<stdio.h>
int onesCount(int n)
{
int onescount=0;
while(n>0)
{
if((n & 1)==1)
onescount++;

n=n>>1;
}
return onescount;
}
int main()
{
int num,ans;
printf("Enter Any Number=");
scanf("%d",&num);
ans=onesCount(num);
if(ans%2==0)
printf("Ram wins");
else
printf("Mouni wins");
return 0;
}

6. Activities 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 when
faculty tell a particular key word (Ex MSB).
7. Examples & Contemporary Extracts of Articles / Practices to convey the idea of the Session

We use the bitwise operators in C to perform operations numeric values at a bit level.
Performing a bitwise operation is also called bit-level programming. To perform numerical
computations for faster we use bitwise because it has only two digits 1 or 0.

https://www.programiz.com/c-programming/bitwise-operators

8. Table Numbering

--

9. Figures with caption

--

10. Self-Assessment Questions (SAQs)

1. List the bit wise operators.

2 Illustrate the manually procedure for 5<<2, 5>>2, ~5.

3 Explain how to represent negative number in 2’s complement.

4 Write formulas for left, right shift operators, bit wise complement.

5 How to represent decimal in 1’s complement and 2’s complement.

6 Define MSB and LSB.

11. Summary

In this session, we learned about

Different types of bitwise operators and their usage. By using bitwise we perform arthimatic
operations at bit level. Operators are << left shift , >> right shift ,~ complement .

Performs a bitwise NOT (complement) operation on an integer, resulting in a new integer


with each bit flipped (1 becomes 0 and 0 becomes 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.
12 Terminal Questions

1 implements a Code to swap a two number using bit wise.

2 implement a Code to find even or odd using bit wise.

3 implement a Code to find number of one’s and zero’s in given number using bit wise.

4 implement a Code to inverting the bits of specified number.

5 implement a Code to solve the palindrome using bit wise.

6 implement a Code on flipped bits using bitwise (Ex here a is 8 -- 1000 b is 15 ---1111 . how
many bits u have to flip to get b. a has to flipped 3 bits)

13. Case Studies

Encryption and compression: Image compression or data encryption algorithms like AES
and DES where we perform on bit wise.

A finite-state machine (FSM) or finite-state automata is simply a state machine, is a


mathematical model of computation. machine that can be in exactly one of a finite number of
states . Either by taking 0 or 1 it will change the state.

14. References of Books / Websites / Link

The C Programming Language by Brian Kernighan and Dennis Ritchie 2nd edition pearson
publication - This is the classic book on C programming and is a great resource for learning
about functions in C.

C: THE COMPLETE REFERENCE McGraw Hill Education; 4th edition by Herb Scheldt.

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


Kanetkar BPB publication .

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


resource for learning C programming, including functions.

C Functions" by Michael J. Misamore - This book is focused specifically on functions in C


and can be a great resource for those who want to deepen their understanding of this
topic
Sites and Web links:

https://www.codecademy.com/resources/docs/cpp/bitwise-operators

https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/

https://www.programiz.com/c-programming/bitwise-operators

15. Keywords

<< left shift

>> right shift

~ complement.

2’s complement

1’s complement

MSB

LSB

Sign Bit

Negative number representation in bit level

You might also like